Skip to content

Instantly share code, notes, and snippets.

@jtkorb
Created March 6, 2013 13:41
Show Gist options
  • Save jtkorb/5099368 to your computer and use it in GitHub Desktop.
Save jtkorb/5099368 to your computer and use it in GitHub Desktop.
/**
* Exception Demonstration
*
* Run main method with
*
* no arguments
* 0
* 1
* hello
*
* and watch (and explain!) the output.
*
* @author jtk
*
* @date 3/6/2013
*/
public class ExceptionDemo {
public static void main(String[] args) {
try {
int x = Integer.parseInt(args[0]);
System.out.printf("try: x = %d\n", x);
int z = 42 / x;
System.out.printf("try: z = %d\n", z);
return;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException: " + e.getMessage());
} catch (NumberFormatException e) {
System.out.println("NumberFormatException: " + e.getMessage());
} catch (ArithmeticException e) {
System.out.println("ArithmeticException: " + e.getMessage());
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
} finally {
System.out.println("finally");
}
System.out.println("end");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment