Throws :
We can handle exception handling by try/catch blocks or throws keywords.
If we want to handle the exception on the risky code within the same method then we will use try catch.
If we want to redirect to the caller to handle the exception which calls the method which can cause exception or Error, then the method which is called by the caller will be given throws keyword.
Lets do a simple example on throws keyword.
from the above class we can see, we are not handling the code in getNumber method instead we are throwing it
We can see the main method which is the caller is calling the getNumber() method but we are handling it by try/catch.
Output:
we can run the code, the method getNumber() will raise an Arithmetic Exception which is a subclass of Exception, but it is handled by the caller. which is as below
a method can throw one or more exception types using throws keyword by comma separated values
Where to use throws keyword?
throws keyword can be used at
a. method
b. constructor.
Note: We can use only throwable objects to throw exceptions using throws keyword.
Problems using the throws keywords:
If the caller is not handling the exception which is cause by the called method, then the application will terminate immediately
Lets simulate it with a small example
In the above class, We are throwing Arithmetic Exception for getNumber().
now lets run the class and see the output.
Output:
Since the main method didn't handle the code, Default exception handler handled the exception and JVM terminated the application.
Note: it is always recommended to use try/catch block instead of throws keyword, as throws keyword will not help in graceful termination of the application.
We can handle exception handling by try/catch blocks or throws keywords.
If we want to handle the exception on the risky code within the same method then we will use try catch.
If we want to redirect to the caller to handle the exception which calls the method which can cause exception or Error, then the method which is called by the caller will be given throws keyword.
Lets do a simple example on throws keyword.
package com.learning.test;
public class Test {
public static void main(String[] args) {
try {
getNumber();
} catch (Exception e) {
System.out.println("Exception handled by the caller");
}
System.out.println("Addition : "+(10+10));
}
public static void getNumber() throws Exception{
System.out.println(10 / 0);
}
}
from the above class we can see, we are not handling the code in getNumber method instead we are throwing it
public static void getNumber() throws Exception {
We can see the main method which is the caller is calling the getNumber() method but we are handling it by try/catch.
try {
getNumber();
} catch (Exception e) {
System.out.println("Exception handled by the caller");
}
Now lets run the class see the outputOutput:
Exception handled by the caller
Addition : 20
we can run the code, the method getNumber() will raise an Arithmetic Exception which is a subclass of Exception, but it is handled by the caller. which is as below
a method can throw one or more exception types using throws keyword by comma separated values
public static void getNumber() throws ArithmeticException,IllegalStateException,......etc {
Where to use throws keyword?
throws keyword can be used at
a. method
b. constructor.
Note: We can use only throwable objects to throw exceptions using throws keyword.
Problems using the throws keywords:
If the caller is not handling the exception which is cause by the called method, then the application will terminate immediately
Lets simulate it with a small example
package com.learning.test;
public class Test {
public static void main(String[] args) {
getNumber();
System.out.println("Addition : "+(10+10));
}
public static void getNumber() throws ArithmeticException {
System.out.println(10 / 0);
}
}
In the above class, We are throwing Arithmetic Exception for getNumber().
public static void getNumber() throws ArithmeticException {
But the caller main method is calling getNumber() method but not handling the exception.now lets run the class and see the output.
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.learning.test.Test.getNumber(Test.java:13)
at com.learning.test.Test.main(Test.java:7)
Since the main method didn't handle the code, Default exception handler handled the exception and JVM terminated the application.
Note: it is always recommended to use try/catch block instead of throws keyword, as throws keyword will not help in graceful termination of the application.