Catch Multiple Exceptions, Rethrow Exceptions in Java
The exception is an unwanted unexpected event that disturbs the normal flow of the program is called an exception.
What is the meaning of exception handling?
Exception handling doesn't mean repairing an exception. We have to define an alternative way to continue the rest of the program normally this way of defining alternative is nothing but exception handling".
In this tutorial, we will see how to catch the multiple exceptions in Java.
Multi catch block
If the Multiple Exceptions having the same handling code, still we have to write a separate catch block for every exception, it increases the length of the code and reviews readability.
try{
//code causes exception
}
catch(ArithmeticException e) {
e.printStackTrace();
}
catch(NullPointerException e) {
e.printStackTrace();
}
catch(ClassCastException e) {
System.out.println(e.getMessage());
}
catch(IOException e) {
System.out.println(e.getMessage());
}
catch(Exception e) {
System.out.println(e.getMessage());
}
To avoid multiple duplicate codes, the Multi catch block concept is introduced in Java 1.7.
The benefit of multiple catch blocks is that we can write a single catch block, which can handle multiple different exceptions.
try{
//code causes exception
}
catch(ArithmeticException | NullPointerException e) {
e.printStackTrace();
}
catch(ClassCastException | IOException e) {
System.out.println(e.getMessage());
}
In a multi-catch block. We have to follow the rule. There should not be any relation between Exception types(either child to parent Or parent to child Or the same type, otherwise we will get Compile time error.
try{
//code causes exception
}
catch(ArithmeticException | Exception e) { // compile time error
e.printStackTrace();
}
Rethrowing an Exception
For converting the exception of one type to another type we have throw keyword. Using the throw keyword, we can use a rethrow exception.
package com.javacodestuffs.core.exception;
public class RethrowException {
public static void main(String[] args) {
try {
System.out.println(10 / 0);
}
catch(ArithmeticException e) {
throw new NullPointerException();
}
}
}
output:
Exception in thread "main" java.lang.NullPointerException
at com.javacodestuffs.core.exception.RethrowException.main(RethrowException.java:9)
Exception Propagation in Java
If inside a method if an exception raised and if that method doesn't handle that exception, then the Exception object will be propagated to the caller. then the caller method is responsible to handle that exceptions. This process is called Exception Propagation.
package com.javacodestuffs.core.exception;
public class ExceptionPropagationExample {
void display3(){
int result = 100 / 0;
}
void display2(){
display3();
}
void display1(){
try{
display2();
} catch(Exception e){
System.out.println("Exception caught in display2...");
}
}
public static void main(String args[]){
ExceptionPropagationExample exp = new ExceptionPropagationExample();
exp.display1();
System.out.println("After exception handling...");
}
}
output:
Exception caught in display2...
After exception handling...
We can see that
The exception has occurred in the display3() method and in display3() method we don't have any exception handler code.
The uncaught exception will be propagated downward in stack i.e it will check appropriate exception handler in the display2() method.
Again in the display2 method, we don't have any exception handler then again exception is propagated downward to display1() method where it finds exception handler.
Thus we can see that uncaught exception is propagated in the stack until stack becomes empty.
Post/Questions related to Catch Multiple Exceptions, Rethrow Exceptions in Java
How to return string array in java?
How to handle exception in java 8?
Java exceptions can be handled by 5 keywords
How to print the exception in java?
When does exception occur in Java?
What are the ways to handle exceptions in Java?
To know more about Exception Handling in Java, follow the tutorial
In this article, we have seen how to Catch Multiple Exceptions, Rethrow Exceptions in Java.
0 Comments
Post a Comment