Throw and Throws

  • Eclipse: Oxygen
  • Java: 1.8

Throw and Throws

Throws are used to suppress the error if the user doesn’t want to specify the try and catch block. Note that this doesn’t handle the error only suppresses it. It should be used only when the developer is sure that the error doesn’t occur.

The throw is used to forcefully throw an error where it is decided by the developer regarding the certain conditions.

The keyword “throw” is used to throw an exception from any method or static block, while the keyword “throws”, used in a method declaration, indicates what exception can be thrown with this method. They are not interchangeable.

App class

Output

Message from someMethod
catch block of main method (Exception)

Contributed by Poonam Tomar


VIEW COURSES

Buy me a coffee!

 

PayTM QR (INDIA)

  • Get video tutorials
  • Project files set
  • Get Certified

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu