

























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


























































Chapter Seventeen Modern Programming Languages, 2nd ed. (^) Docsity.com 1
Chapter Seventeen Modern Programming Languages, 2nd ed. 2
public class Test { public static void main(String[] args) { int i = Integer.parseInt(args[0]); int j = Integer.parseInt(args[1]); System.out.println(i/j); } }
javac Test.java java Test 6 3 2
Docsity.com
Chapter Seventeen Modern Programming Languages, 2nd ed. (^) Docsity.com 4
Chapter Seventeen Modern Programming Languages, 2nd ed. 5
Java Exception Code to Cause It
NullPointerException String s = null; s.length(); ArithmeticException int a = 3; int b = 0; int q = a/b; ArrayIndexOutOfBoundsException int[] a = new int[10]; a[10];
ClassCastException Object x = new Integer(1); String s = (String) x; StringIndexOutOfBoundsException String s = "Hello"; s.charAt(5);
Docsity.com
Chapter Seventeen Modern Programming Languages, 2nd ed. (^) Docsity.com 7
Chapter Seventeen Modern Programming Languages, 2nd ed. 8
Java will only throw objects of a class descended from Throwable
Classes derived from Error are used for serious, system- generated errors, like OutOfMemoryError, that usually cannot be recovered from
Classes derived from Exception are used for ordinary errors that a program might want to catch and recover from
Classes derived from RuntimeException are used for ordinary system- generated errors, like ArithmeticException
Exception
Object
Throwable
Error
RuntimeException
…
…
…
Docsity.com
Chapter Seventeen Modern Programming Languages, 2nd ed. 10
Docsity.com
Chapter Seventeen Modern Programming Languages, 2nd ed. 11
public class Test { public static void main(String[] args) { try { int i = Integer.parseInt(args[0]); int j = Integer.parseInt(args[1]); System.out.println(i/j); } catch (ArithmeticException a) { System.out.println("You're dividing by zero!"); } } } This will catch and handle any^ ArithmeticException. Other exceptions will still get the language system’s default behavior.
Docsity.com
Chapter Seventeen Modern Programming Languages, 2nd ed. (^) Docsity.com 13
Chapter Seventeen Modern Programming Languages, 2nd ed. 14
System.out.print("1, "); try { String s = null; s.length(); } catch (NullPointerException e) { System.out.print("2, "); } System.out.println("3");
Docsity.com
Chapter Seventeen Modern Programming Languages, 2nd ed. 16
Docsity.com
If z throws an exception
it does not catch, z’s
activation stops…
…then y gets a chance to
catch it; if it doesn’t, y’s
activation stops…
…and so on all the way
back to f
Chapter Seventeen Modern Programming Languages, 2nd ed. 17
y’s activation record
z’s activation record
f’s activation record
g’s activation record
...
Docsity.com
Chapter Seventeen Modern Programming Languages, 2nd ed. 19
Docsity.com
Chapter Seventeen Modern Programming Languages, 2nd ed. 20
public static void main(String[] args) { try { int i = Integer.parseInt(args[0]); int j = Integer.parseInt(args[1]); System.out.println(i/j); } catch (ArithmeticException a) { System.out.println("You're dividing by zero!"); } catch (ArrayIndexOutOfBoundsException a) { System.out.println("Requires two parameters."); } } (^) This will catch and handle both ArithmeticException and ArrayIndexOutOfBoundsException
Docsity.com