Java Multiple Catch Block


A try block can be followed by multiple catch blocks.

 

Each catch block contains a different exception can be handled.

Syntax:

 try {
	
}
catch (ExceptionName e) {  

}
catch (ExceptionName e1) {
  
}

Example

public class MultipleCatch{
    public static void main(String args[]){  
        try{  
            int a=1/0;  
        }
        catch(NullPointerException e){
            System.out.println(e);
        } 
        catch(ArithmeticException e){
            System.out.println(e);
        } 
        catch(Exception e){
            System.out.println(e);
        } 
        finally{
            System.out.println("Always execute");  
        }
  	}  
} 



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.