Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Java Errors and Exceptions Because Murphy’s Law never fails 1 “Java is the most distressing thing to hit computing since MS-DOS.” – Alan Kay 2 Corresponding Book Sections • Pearson Custom Computer Science: Chapter 2, Sections 1-7 • Data Structures: Sections 2.3 3 Programs Crash • Programs can fail for any number of reasons, including invalid input, failed hardware, or even bad programming! • Fortunately, Java has some mechanisms to deal with failure 4 Types of Exceptions/ Errors Image from Pearson’s Textbook, page 39 5 Java Errors • Errors - generally happen when the Java runtime encounters a serious problem. A programmer is generally not expected to handle these. • Examples: • OutOfMemoryError - occurs when the JVM cannot allocate anymore memory for the program to use • StackOverflowError - occurs if you call too many methods consecutively and therefore run out of stack space. Usually occurs when using recursion when doing the recursive equivalent of an infinite loop 6 Java Exceptions • Generally happen when a Java program encounters a problem • A programmer is generally expected to handle these according to the Java API, and the Java compiler (and therefore Eclipse) will yell at you to do so • Exception to expected handled exceptions is Runtime Exceptions 7 Runtime Exceptions • NullPointerException - Referencing an object that is not there • ArrayIndexOutOfBoundsException - Accessing an array index that does not exist • UndeclaredThrowableException - when a method throws an exception (actually any throwable object) of some type that it is not declared to throw 8 Other Exceptions - input / output failed or • IOException stopped working - you referenced • ClassNotFoundException a class that was not created/imported into • your program UnsupportedFlavorException - happens when you ask to try the 32nd flavor at Baskin Robbins (actually for when a DataFlavor type is not supported for a given request, such as a copy/paste operation) 9 Handling Exceptions • Java has some built in keywords and objects for handling program problems • Handling exceptions well will allow you to: • Help users of your program realize why your program is not functioning the way they think it should be • Allow you to have your program recover from issues that would normally make it crash 10 Exception Handling Keywords • throw - manually throw an exception, exits the current method you are in when it is used • throws - specifies that a method can throw this type of exception, used after a method is declared 11 public class Arithmetic { public static int quotient(int number1, int number2) throws ArithmeticException { if (number2 == 0) throw new ArithmeticException("Divisor cannot be zero"); return number1 / number2; } public static void main(String[] args) { Scanner input = new Scanner(System.in); int number1 = input.nextInt(); int number2 = input.nextInt(); } } try { int result = quotient(number1, number2); System.out.println(number1 + "/" + number2 + " is " + result); } catch (ArithmeticException ex) { System.out .println("Exception: an integer cannot be divided by zero "); } finally { System.out.println("Execution continues ...."); } From Pearson’s Textbook, Page 37 12 public class Arithmetic { public static int quotient(int number1, int number2) throws ArithmeticException { if (number2 == 0) throw new ArithmeticException("Divisor cannot be zero"); return number1 / number2; } public static void main(String[] args) { Scanner input = new Scanner(System.in); int number1 = input.nextInt(); int number2 = input.nextInt(); } } try { int result = quotient(number1, number2); System.out.println(number1 + "/" + number2 + " is " + result); } catch (ArithmeticException ex) { System.out .println("Exception: an integer cannot be divided by zero "); } finally { System.out.println("Execution continues ...."); } From Pearson’s Textbook, Page 37 13 More Keywords! • try - block of code to run that may throw exceptions • catch - block of code to run if a certain type of exception is thrown in the try block • finally - block of code to execute whether an exception has been caught or not 14 public class Arithmetic { public static int quotient(int number1, int number2) throws ArithmeticException { if (number2 == 0) throw new ArithmeticException("Divisor cannot be zero"); return number1 / number2; } public static void main(String[] args) { Scanner input = new Scanner(System.in); int number1 = input.nextInt(); int number2 = input.nextInt(); } } try { int result = quotient(number1, number2); System.out.println(number1 + "/" + number2 + " is " + result); } catch (ArithmeticException ex) { System.out .println("Exception: an integer cannot be divided by zero "); } finally { System.out.println("Execution continues ...."); } From Pearson’s Textbook, Page 37 15 To the Code! 16