Exception handling : throw, throws and Throwable Exception handling : throw, throws and Throwable java java

Exception handling : throw, throws and Throwable


  • 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).

Example:

enter image description here


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

Example:

enter image description here



  • throw: statement to throw object t where t instanceof java.lang.Throwable must be true.
  • throws: a method signature token to specify checked exceptions thrown by that method.
  • java.lang.Throwable: the parent type of all objects that can be thrown (and caught).

See here for a tutorial on using exceptions.


This really easy to understand.

The java.lang.Throwable:

The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause. More

The key word throws is used in method declaration, this specify what kind of exception[Throwable class] we may expect from this method.

The key word throw is used to throw an object that is instance of class Throwable.


Lest see some example:

We create ourself an exception class

public class MyException super Exception {}

The we create a method that create a object from our exception class and throws it using key word throw.

private  void throwMeAException() throws MyException //We inform that this method throws an exception of MyException class{  Exception e = new MyException (); //We create an exception   if(true) {    throw e; //We throw an exception   } }

When we are going to use method throwMeAException(), we are forced to take care of it in specific way because we have the information that it throws something, in this case we have three options.

First option is using block try and catch to handle the exception:

private void catchException() {   try {     throwMeAException();   }   catch(MyException e) {     // Here we can serve only those exception that are instance of MyException   }}

Second option is to pass the exception

   private void passException() throws MyException {       throwMeAException(); // we call the method but as we throws same exception we don't need try catch block.   }

Third options is to catch and re-throw the exception

private void catchException() throws Exception  {   try {     throwMeAException();   }   catch(Exception e) {      throw e;   }}

Resuming, when You need to stop some action you can throw the Exception that will go back till is not server by some try-catch block. Wherever You use method that throws an exception You should handle it by try-catch block or add the declarations to your methods.

The exception of this rule are java.lang.RuntimeException those don't have to be declared. This is another story as the aspect of exception usage.