• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

throw and throws

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks Prabhu !!!
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
  • 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.
 
Trust God, but always tether your camel... to this tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic