Skip to content

Instantly share code, notes, and snippets.

@codinko
Created October 17, 2021 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codinko/be0aea4ee090efbaa8f0bba33faa4e47 to your computer and use it in GitHub Desktop.
Save codinko/be0aea4ee090efbaa8f0bba33faa4e47 to your computer and use it in GitHub Desktop.
package com.learn.java;
import java.util.ArrayList;
import java.util.List;
public class TryFinally {
public static void main(String[] args) {
List a = null;
try {
a = new ArrayList<String>();
a.add(null);
a.get(0).getClass();
} catch (Exception ex) {
System.out.println("caught exception phase1...");
} finally {
//use this block to close the respurces....
System.out.println("reached finally...");
try {
// testing if we can write bad code here as well, inside finally.. well, you can :)
a.get(0).getClass();
} catch (Exception ex) {
// testing if you can have an exception inside finally ... well, you can :)
System.out.println("caught exception phase2 - inside finally !!...");
}
System.out.println("finished finally...");
}
}
}
----------------------------------------
// OUTPUT --------------------
----------------------------------------
caught exception phase1...
reached finally...
caught exception phase2 - inside finally !!...
finished finally...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment