codeaft@codeaft:~$ javac Codeaft.java codeaft@codeaft:~$ java Codeaft
Exception in thread "main" java.lang.NullPointerException
at Codeaft.main(Codeaft.java:6)
codeaft@codeaft:~$
Java program to demonstrate the use of try and catch blocks
Codeaft.java
classCodeaft{publicstaticvoidmain(Stringargs[]){Strings=null;try{System.out.println("Length: "+s.length());}catch(Exceptione){System.out.println("String is null, hence, Unable to find the length.");}}}
Output
codeaft@codeaft:~$ javac Codeaft.java codeaft@codeaft:~$ java Codeaft
String is null, hence, Unable to find the length.
codeaft@codeaft:~$
Java program to demonstrate the use of try with multiple catch blocks
Codeaft.java
classCodeaft{publicstaticvoidmain(Stringargs[]){Strings=null;try{System.out.println("Length: "+s.length());}catch(ArithmeticExceptione){System.out.println("Exception caught in the first catch block");}catch(NullPointerExceptione){System.out.println("Exception caught in the second catch block");}catch(Exceptione){System.out.println("Exception caught in the third catch block");}}}
Output
codeaft@codeaft:~$ javac Codeaft.java codeaft@codeaft:~$ java Codeaft
Exception caught in the second catch block
codeaft@codeaft:~$
Java program to handle the NullPointerException using Exception class
Codeaft.java
classCodeaft{publicstaticvoidmain(Stringargs[]){Strings=null;try{System.out.println("Length: "+s.length());}catch(ArithmeticExceptione){System.out.println("Exception caught in the first catch block");}catch(StringIndexOutOfBoundsExceptione){System.out.println("Exception caught in the second catch block");}catch(Exceptione){System.out.println("Exception caught in the third catch block");}}}
Output
codeaft@codeaft:~$ javac Codeaft.java codeaft@codeaft:~$ java Codeaft
Exception caught in the third catch block
codeaft@codeaft:~$
Java program to handle the NullPointerException using RuntimeException class
Codeaft.java
classCodeaft{publicstaticvoidmain(Stringargs[]){Strings=null;try{System.out.println("Length: "+s.length());}catch(ArithmeticExceptione){System.out.println("Exception caught in the first catch block");}catch(RuntimeExceptione){System.out.println("Exception caught in the second catch block");}catch(Exceptione){System.out.println("Exception caught in the third catch block");}}}
Output
codeaft@codeaft:~$ javac Codeaft.java codeaft@codeaft:~$ java Codeaft
Exception caught in the second catch block
codeaft@codeaft:~$
The following sequence of multiple catch blocks are not allowed
Codeaft.java
classCodeaft{publicstaticvoidmain(Stringargs[]){Strings=null;try{System.out.println("Length: "+s.length());}catch(Exceptione){System.out.println("Exception caught in the third catch block");}catch(RuntimeExceptione){System.out.println("Exception caught in the second catch block");}catch(ArithmeticExceptione){System.out.println("Exception caught in the first catch block");}}}
Output
codeaft@codeaft:~$ javac Codeaft.java codeaft@codeaft:~$ java Codeaft
Codeaft.java:14: error: exception RuntimeException has already been caught
catch(RuntimeException e)
^
Codeaft.java:18: error: exception ArithmeticException has already been caught
catch(ArithmeticException e)
^
2 errors
codeaft@codeaft:~$
Dear User, Thank you for visitng Codeaft. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.