Created
October 17, 2021 17:55
-
-
Save codinko/be0aea4ee090efbaa8f0bba33faa4e47 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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