The throw and throws are the keywords used in exception handling. The throw keyword is used to handover the instance of the exception created by the programmer to the JVM manually. The throws keyword used to handover the responsibility of handling the exception occurred in the method to the caller method.
The basic difference between the throw and throws is that the throw keyword uses the exception object whereas the throws keyword uses the name of the exception classes.
Content: throw Vs throws
Comparison Chart
Basis of Comparison | throw | throws |
---|---|---|
Basic | The throw keyword handover our created exception object to JVM manually. | The throws keyword is used to delegate the responsibility of exception handling to the caller of the method. |
Syntax | throw Throwable-instance; | return_type method_name(parameter-list) throws ExceptionClass_list { // body of method } |
Followed by | The throw keyword is followed by exception object. | The throws keyword is followed by the list of the exception classes that can occur in the method. |
Number of Exception thrown | The throw keyword can throw a single exception instance. | The throws keyword can declare multiple exception classes separated by a comma. |
Definition of throw
The keyword “throw” is used to hand over our created exception instance to the JVM (Java Virtual Machine) manually. If “throw” is not used to throw an exception instance and the exception occurs, then the runtime system internally throws the exception instance to JVM and the program terminates abnormally. The general form of the throw keyword is:
throw Throwable_instance;
Above the Throwable_instance must be an object of the class Throwable. Primitive types such as int, float, or char and the non-throwable class instance can not be thrown using throw keyword.
Let us take an example to understand the keyword throw.
Class Test{ Public static void main(String[ ] args){ throw new ArithmeticException("/ by zero"); } }
In above code, the keyword throw throws an instance of the Exception class “ArithmeticException”. If the throw keyword had not been used then, the main( ) method would have internally created an exception object handed over to the JVM.
The points to remember about the keyword throw:
- It handovers the exception object manually to the JVM.
- It is best used for the user-defined exceptions or customised exceptions.
- If the memory is not allocated to the exception object thrown by throw keyword, then there occurs a runtime exception, NullPointerException .
- The throw keyword stops the execution of the program immediately after its occurrence. we can not directly write any statement after the throw statement. If we write any staement directly after throw statement then the compiler will show an error, unreachable statement during compilation.
- Only objects of Throwable class can be thrown using throw keyword. If the object thrown is not an object of class Throwable then we get a compile time error “Incompatible type found . . required java.lang.Throwable”
Note
The throw keyword is used in C++, JAVA, C#, to manually throw an exception.
Definition of throws
The “throws” keyword is used to delegate the responsibility of handling the exception occurred in the method, to its caller method. The caller method is responsible for handling the exception it may be any other method or JVM. It declares the list of the exception classes that may occur in the method.
The usage of the throws keyword convinces the compiler that the exception occurred in the method is to be handled by the caller method hence, no compilation error occurs. But, the caller method must handle the exception or delegate the responsibility to handle the exception to its hierarchy method.
When the runtime exception occurs, then even after the use of throws keyword, it does not prevent the abnormal termination of the program. If the caller method is main( ), the by default JVM handles the exception.
The general form of the throws keyword is:
return_type method_name(parameter-list) throws exceptionClass_list { // body of method }
We can see that throws keyword appears after the signature of the method, and it can contain the list of exception classes that may occur in the method. The list of the exception classes written after the keyword throws is separated by the comma.
Let us take one example to understand the throws keyword.
calss Test{ public static void main( String[ ] args) throws InterruptedException{ thread sleep( 10000); } }
In the above code, the main thread is made to sleep for some time using the sleep( ) method. Now, when the main method is at sleep it is possible that the other threads may interrupt the main thread. But, the throws keyword is used in after the signature of main( ) method so, the program would compile easily.
The throws keyword is declaring the checked exception class InterruptedException. Now, if any other thread interrupts the main thread during runtime the throws keyword would handover that exception to the caller of main( ) method, which is JVM. The JVM would terminate the program abnormally.
The points to remember about throws keyword:
- The throws keyword is only used for declaring the checked exception classes. The usage of the throws keyword for an unchecked exception has no impact.
- If the method does not want to handle the exception on its own, it delegates that exception to the caller method of that class by using the throws keyword.
- Its usage only allows smooth compilation of the program.
- If an exception occurs at runtime, the program terminates abnormally, even after the usage of the throws keyword.
- It is recommended to use try/catch block for normal termination of the program if an exception occurs at runtime.
Note
The keyword throws used only in Java. C++ and C# do not use the throws keyword.
Key Differences Between throw and throws
- The keyword throw, handover the responsibility of exception handling to JVM manually whereas, the keyword throws, handover the responsibility of exception handling to the caller method of the code where an exception has occurred.
- The throw keyword is followed by the exception object that it hand over to the JVM. On the other hand, throws keyword is followed by the exception classes that can occur in the method.
- The throw keyword can throw a single exception object at a time whereas, the throws keyword can declare multiple exception classes separated by a comma at a time.
Conclusion
The throw keyword is best used for the customised exception. The try/catch block is best for handling the exceptions as compared throws keyword.
Neelam says
Very well written and explained.