17 - Click to edit Master subtitle style Chapter Seventeen...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Click to edit Master subtitle style Chapter Seventeen Modern Programming Languages, 2nd ed. A Third Look At Java Chapter Seventeen Modern Programming Languages, 2nd ed. 11 Chapter Seventeen Modern Programming Languages, 2nd ed. A Little Demo Chapter Seventeen Modern Programming Languages, 2nd ed. 22 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 > Chapter Seventeen Modern Programming Languages, 2nd ed. Exceptions Chapter Seventeen Modern Programming Languages, 2nd ed. 33 > java Test Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Test.main(Test.java:3) > java Test 6 0 Exception in thread "main" java.lang.ArithmeticException: / by zero at Test.main(Test.java:4) In early languages, thats all that happened: error message, core dump, terminate. Modern languages like Java support exception handling . Chapter Seventeen Modern Programming Languages, 2nd ed. Outline 17.2 Throwable classes 17.3 Catching exceptions 17.4 Throwing exceptions 17.5 Checked exceptions 17.6 Error handling 17.7 Finally 17.8 Farewell to Java Chapter Seventeen Modern Programming Languages, 2nd ed. 44 Chapter Seventeen Modern Programming Languages, 2nd ed. Some Predefined Exceptions Chapter Seventeen Modern Programming Languages, 2nd ed. 55 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); Chapter Seventeen Modern Programming Languages, 2nd ed. An Exception Is An Object The names of exceptions are class names, like NullPointerException Exceptions are objects of those classes In the previous examples, the Java language system automatically creates an object of an exception class and throws it If the program does not catch it, it terminates with an error message Chapter Seventeen Modern Programming Languages, 2nd ed. 66 Chapter Seventeen Modern Programming Languages, 2nd ed. Throwable Classes To be thrown as an exception, an object must be of a class that inherits from the predefined class Throwable There are four important predefined classes in that part of the class hierarchy: Throwable Error Exception RuntimeException Chapter Seventeen Modern Programming Languages, 2nd ed. 77 Chapter Seventeen Modern Programming Languages, 2nd ed. Chapter Seventeen Modern Programming Languages, 2nd ed....
View Full Document

This note was uploaded on 09/30/2011 for the course CS 6371 taught by Professor Hamlen during the Spring '11 term at University of Texas at Dallas, Richardson.

Page1 / 65

17 - Click to edit Master subtitle style Chapter Seventeen...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online