Java program to demonstrate the use of finally block
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");}finally{System.out.println("Finally block always get executed");}}}
Output
kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
Exception caught in the second catch block
Finally block always get executed
kodingwindow@kw:~$
The following try and finally block sequence is allowed
KW.java
classKW{publicstaticvoidmain(Stringargs[]){Strings=null;try{System.out.println("Length: "+s.length());}finally{System.out.println("Finally block always get executed");}}}
Output
kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
Finally block always get executed
Exception in thread "main" java.lang.NullPointerException
at KW.main(KW.java:8)
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