Sumithra Sumithra - 3 years ago 186
Java Question

Exception handling : throw, throws and Throwable

Can any of you explain what the differences are between

and when to use which?

Answer Source
  • throws: Used when writing methods, to declare that the method in question throws the specified (checked) exception.

    As opposed to checked exceptions, runtime exceptions (NullPointerExceptions etc) may be thrown without having the method declare throws NullPointerException.

  • throw: Instruction to actually throw the exception. (Or more specifically, the Throwable).

    The throw keyword is followed by a reference to a Throwable (usually an exception).


enter image description here

  • Throwable: A class which you must extend in order to create your own, custom, throwable.


enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download