Chapter 15 Exceptions and assertions Prerequisites for part Iv Chapter 8 Inheritance and Polymorphism Chapter 15 Exceptions and assertions Chapter 16 Simple Input and Output Nothing is impossible Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 15 Exceptions and Assertions Prerequisites for Part IV Chapter 8 Inheritance and Polymorphism Chapter 16 Simple Input and Output Chapter 15 Exceptions and Assertions Nothing is impossible
Objectives To know what is exception and what is exception handling (§152) To distinguish exception types Error(fatal)vs. Exception(non- fatal), and checked VS. uncheck exceptions(8 15.2) To declare exceptions in the method header($ 15.3) ● To throw exceptions out of a method(§15.3 o To write a try-catch block to handle exceptions($ 15.3 o To explain how an exception is propagated(8 15. 3) o To rethrow exceptions in a try-catch block(8 15.4) o To use the finally clause in a try-catch block($ 15.5) ● To know when to use exceptions(§156) o to declare custom exception classes(8 15.7 Optional) tional o To apply assertions to help ensure program correctness(8 15.8) Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 2 Objectives ⚫ To know what is exception and what is exception handling (§15.2). ⚫ To distinguish exception types: Error (fatal) vs. Exception (nonfatal), and checked vs. uncheck exceptions (§15.2). ⚫ To declare exceptions in the method header (§15.3). ⚫ To throw exceptions out of a method (§15.3). ⚫ To write a try-catch block to handle exceptions (§15.3). ⚫ To explain how an exception is propagated (§15.3). ⚫ To rethrow exceptions in a try-catch block (§15.4). ⚫ To use the finally clause in a try-catch block (§15.5). ⚫ To know when to use exceptions (§15.6). ⚫ To declare custom exception classes (§15.7 Optional). ⚫ To apply assertions to help ensure program correctness (§15.8)
Syntax Errors Runtime Errors, and Logic errors there are three categories of errors: syntax errors runtime errors, and logic errors o Syntax errors arise because the rules of the language have not been followed They are detected by the compiler o Runtime errors occur while the program is running if the environment detects an operation that is impossible to carry out o Logic errors occur when a program doesn 't perform the way it was intended to Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 3 Syntax Errors, Runtime Errors, and Logic Errors there are three categories of errors: syntax errors, runtime errors, and logic errors. ⚫Syntax errors arise because the rules of the language have not been followed. They are detected by the compiler. ⚫Runtime errors occur while the program is running if the environment detects an operation that is impossible to carry out. ⚫ Logic errors occur when a program doesn't perform the way it was intended to
Runtime errors mport javax. swing. vOptionpane; public class Test public static void (String [] args)t String input JoptionPane show InputDialog(null Please enter an integer) int number nteger parse If an exception occurs on this line. the rest lines in the method / Display the result are skipped and the program is terminated JOptionPane showMessageDialog(null The number entered is+ number) System. exit(0) V Terminated duction to Java
Liang,Introduction to Java Programming,revised by Dai-kaiyu 4 Runtime Errors import javax.swing.JOptionPane; public class Test { public static void main(String[] args) { String input = JOptionPane.showInputDialog(null, "Please enter an integer"); int number = Integer.parseInt(input); // Display the result JOptionPane.showMessageDialog(null, "The number entered is " + number); System.exit(0); } } If an exception occurs on this line, the rest lines in the method are skipped and the program is terminated. Terminated
Catch Runtime errors mport javax. swing. JOptionPane; ublic class test public static void main(String[] args) try i stril t= JOptionPane. showInput Dialog(null Please enter an int number Integer parseInt(input)i If an exception occurs on this line, the rest lines in the try clause are skipped and the control is transferred to the catch clause // Display the result JOptionPane showMessageDialog(null The number entered is+ number JOptionPane showMessageDialog(null Incorrect input: an integer is required")i After the exception is caught and processed, the control is transferred to the next statement after the try-catch block System. out. println(" Execution continues System. exit(0) duction to Java
Liang,Introduction to Java Programming,revised by Dai-kaiyu 5 Catch Runtime Errors import javax.swing.JOptionPane; public class Test { public static void main(String[] args) { try { String input = JOptionPane.showInputDialog(null, "Please enter an integer"); int number = Integer.parseInt(input); // Display the result JOptionPane.showMessageDialog(null, "The number entered is " + number); } catch (Exception ex) { JOptionPane.showMessageDialog(null, "Incorrect input: an integer is required"); } System.out.println("Execution continues ..."); System.exit(0); } } If an exception occurs on this line, the rest lines in the try clause are skipped and the control is transferred to the catch clause. After the exception is caught and processed, the control is transferred to the next statement after the try-catch block
Exception Classes ClassNotFoundException IOEXception Arithmetic Excepti Exception AWTException HNullPointer Exception] RuntimeException IndexOutofBoundsException Object Throwab Several more classes IllegalArgument Exception Linkage error Several more classes Ⅴ irtualMachine Error Error AWTError Several more classes Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 6 Exception Classes LinkageError Error AWTError AWTException Throwable ClassNotFoundException VirtualMachineError IOException Exception RuntimeException Object ArithmeticException NullPointerException IndexOutOfBoundsException Several more classes Several more classes Several more classes IllegalArgumentException
System Errors ClassNot Found Exception IOExcepti Arithmetic Exception Exception AWTException lullPointer Excepti RuntimeException IndexOutofBounds Exception Object Throwal Several more classes IllegalArgument Exception System errors are thrown LinkageError Several more classes by/VM and represented in the error class The error VirtualMachine Error class describes internal Error system errors. Such errors AWTErro rarely occur. If one does there is little you can do Several more classes beyond notifying the user and trying to terminate the program gracefully Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 7 System Errors LinkageError Error AWTError AWTException Throwable ClassNotFoundException VirtualMachineError IOException Exception RuntimeException Object ArithmeticException NullPointerException IndexOutOfBoundsException Several more classes Several more classes Several more classes IllegalArgumentException System errors are thrown by JVM and represented in the Error class. The Error class describes internal system errors. Such errors rarely occur. If one does, there is little you can do beyond notifying the user and trying to terminate the program gracefully
Exceptions Exceptions are represented ClassNot Found Exception in the exception class that describes errors caused by IOException our program and external Arithmetic Exception circumstances These errors Exception AWTException can be caught and handled by INullPointer Except your program RuntimeException IndexOutOfBounds Exception hrowab Several more classes IllegalArgument Exception LinkageError Several more classes Virtualmachine erro E rror AWTError Several more classes Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 8 Exceptions LinkageError Error AWTError AWTException Throwable ClassNotFoundException VirtualMachineError IOException Exception RuntimeException Object ArithmeticException NullPointerException IndexOutOfBoundsException Several more classes Several more classes Several more classes IllegalArgumentException Exceptions are represented in the Exception class that describes errors caused by your program and external circumstances. These errors can be caught and handled by your program
Runtime exceptions ClassNotFound Exception IOException Arithmetic Exception Exception AWTException NullPointerException RuntimeException IndexOutofBounds Exception Object Throwable Several more classes IllegalArgument Exception Linkage error Several VirtualMachine Error Error Runtime exceptions are AWTError represented in the RuntimeException class that Several more classes describes programming errors, such as bad casting access ing an out-of- bounds array, and numeric errors Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 9 Runtime Exceptions LinkageError Error AWTError AWTException Throwable ClassNotFoundException VirtualMachineError IOException Exception RuntimeException Object ArithmeticException NullPointerException IndexOutOfBoundsException Several more classes Several more classes Several more classes IllegalArgumentException Runtime exceptions are represented in the RuntimeException class that describes programming errors, such as bad casting, accessing an out-ofbounds array, and numeric errors
Checked Exceptions Vs Unchecked eD Exceptions ORuntimeException, Error and their subclasses are known as unchecked exceptions oAll other exceptions are known as checked exceptions, meaning that the compiler forces the programmer to check and deal with the exceptions Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 10 Checked Exceptions vs. Unchecked Exceptions ⚫RuntimeException, Error and their subclasses are known as unchecked exceptions. ⚫All other exceptions are known as checked exceptions, meaning that the compiler forces the programmer to check and deal with the exceptions