Last active
December 2, 2019 18:00
-
-
Save AnujJha-stack/e4f99685aa960dd71c562e78b875976b to your computer and use it in GitHub Desktop.
Java_ExceptionHandling
This file contains hidden or 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
//default throw our catch | |
public class ExpHand1 { | |
public static void main(String[] a) { | |
try { | |
System.out.println("INSIDE TRY l1"); | |
String s = null; | |
int len = s.length();//null pointer exception | |
// int tmp = 5 / 0;//arithmetic exception | |
System.out.println("INSIDE TRY l3");//not executed | |
} | |
catch(NullPointerException | ArithmeticException x){ | |
System.out.println("Exception: "+x.getMessage()); | |
} | |
//finally is always executed after our catch or just before default catch. | |
finally { | |
System.out.println("INSIDE FINALLY"); | |
} | |
} | |
} |
This file contains hidden or 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
//our throw our catch | |
public class ExpHand2 { | |
public static void main(String[] args){ | |
int bal = 7000; | |
int wdl = 8000;//input from user. | |
try{ | |
if(bal < wdl){ | |
throw new ArithmeticException("Insufficient Funds"); | |
} | |
System.out.println("Txn Successful"); | |
bal-=wdl; | |
System.out.println("Available balance :"+bal); | |
} | |
catch(ArithmeticException e){ | |
System.out.println("Exception : "+e.getMessage()); | |
} | |
finally { | |
System.out.println("Program continues."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment