kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
Exception in thread "main" java.lang.NullPointerException
at KW.main(KW.java:6)
kodingwindow@kw:~$
Java program to demonstrate the use of try and catch blocks
KW.java
classKW{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
kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
String is null, hence, Unable to find the length.
kodingwindow@kw:~$
Java program to demonstrate the use of try with multiple catch blocks
KW.java
classKW{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
kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
Exception caught in the second catch block
kodingwindow@kw:~$
Java program to handle the NullPointerException using Exception class
KW.java
classKW{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
kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
Exception caught in the third catch block
kodingwindow@kw:~$
Java program to handle the NullPointerException using RuntimeException class
KW.java
classKW{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
kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
Exception caught in the second catch block
kodingwindow@kw:~$
The following sequence of multiple catch blocks are not allowed
KW.java
classKW{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
kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
KW.java:14: error: exception RuntimeException has already been caught
catch(RuntimeException e)
^
KW.java:18: error: exception ArithmeticException has already been caught
catch(ArithmeticException e)
^
2 errors
kodingwindow@kw:~$
Dear User, Thank you for visitng KodingWindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.
Dear User, We are grateful for your interest in the KodingWindow Android app. We intend to soon make it available on the Google Play Store. Currently, you need to install apk manually. Would you like to get the app? Minimum Requirement(s): Android 10