Skip to content

Instantly share code, notes, and snippets.

@kenorb
Last active August 29, 2015 14:04
Show Gist options
  • Save kenorb/a6028049e8cd6e346d93 to your computer and use it in GitHub Desktop.
Save kenorb/a6028049e8cd6e346d93 to your computer and use it in GitHub Desktop.
ArithmeticException Demo
/*
* ArithmeticException Demo
*
* Notes:
* - A try block can have one or more catch blocks, or finally block.
*
* Usage:
* javac Excep1.java
* java Excep1
* java Excep1 10 2
* java Excep1 1
* java Excep1 10 0
* java Excep1 x y
*/
class Excep1 {
public static void main(String[] args) {
try {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
System.out.println(a/b);
System.out.println("No Exception");
System.out.println("a = " + a + "; " + "b = " + b);
}
catch (ArithmeticException ae) { // http://docs.oracle.com/javase/7/docs/api/java/lang/ArithmeticException.html
System.out.println("Inside Arithmetic Exception"); // e.g. args: 10 0
}
catch (NumberFormatException nfe) {
System.out.println("Inside Number Format Exception"); // e.g. args: x y
}
catch (ArrayIndexOutOfBoundsException ai) {
System.out.println("Inside Array Index Exception"); // e.g. args: 1
}
System.out.println("Continue the execution");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment