Skip to content

Instantly share code, notes, and snippets.

@AnujJha-stack
Last active December 2, 2019 18:00
Show Gist options
  • Save AnujJha-stack/e4f99685aa960dd71c562e78b875976b to your computer and use it in GitHub Desktop.
Save AnujJha-stack/e4f99685aa960dd71c562e78b875976b to your computer and use it in GitHub Desktop.
Java_ExceptionHandling
//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");
}
}
}
//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