Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

throw and throws

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everyone!

Could anyone please tell me the diffrence between the "throw" and "throws" clauses in exception handling?

thanks in advance,
Ravissant Markenday
 
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
throw - used when you want to throw exception explicitly. you can throw any exception which subclass of Throwable interface.

Example:

throws - used when you think that your method may throw exception, so caller should take care of it.

Example:
This method is from FileInputStream class. It says, if you use read() method then you need to catch IOException.
 
Ravissant Markenday
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Prabhu, so can both "throw" and "throws" be used for checked and unchecked exceptions???

Thanks again,

Warm Regards,
Ravissant Markenday
 
Ravissant Markenday
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes and one more question here, does jvm handle both checked and unchecked exceptions??

Thanks,

Regards,
Ravissant Markenday
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

can both "throw" and "throws" be used for checked and unchecked exceptions???



Yes, you can.However, if throw unchecked exceptions using throw or throws, it is not mandatory for caller to handle it. means caller can call your method without try{}catch(){}.
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

does jvm handle both checked and unchecked exceptions??



How JVM knows, how to handle the exception which occurred in your code?

JVM is the one who throws those exception, saying "Buddy, something wrong with your code, check it!".

hth,
 
Ravissant Markenday
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Prabhu !!!
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One small correction on the first reply to this question - Throwable is not an interface, but a perfectly instantiable class.
 
reply
    Bookmark Topic Watch Topic
  • New Topic