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