Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.

Similar presentations


Presentation on theme: "Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming."— Presentation transcript:

1 Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming

2 SE15: Exceptions27–2 Today’s Learning Objectives For you to appreciate why exceptions are a useful mechanism for signalling errors in programs For you to understand the effect of exceptions on flow of control in a program For you to see how we can intercept exceptions that occur within Java code

3 SE15: Exceptions27–3 Lecture Outline Traditional approaches to error handling A different approach: exceptions Flow of control Dealing with exceptions

4 SE15: Exceptions27–4 Traditional Approaches To Error Handling Ignore it! Caller should do the right thing Terminate the program Call System.exit Transfer responsibility

5 SE15: Exceptions27–5 Example: Ignoring Errors class Date { private int day, month, year;... public void setMonth(int theMonth) { month = theMonth; } }

6 SE15: Exceptions27–6 Example: Program Termination class Date { private int day, month, year;... public void setMonth(int theMonth) { if (theMonth 12) { System.err.println("Invalid month!"); System.exit(1); } month = theMonth; } }

7 SE15: Exceptions27–7 What Do You Think? What is wrong with this approach? Where would it be reasonable to use it?

8 SE15: Exceptions27–8 Transfer Responsibility class Date { private int day, month, year;... public boolean setMonth(int theMonth) { if (theMonth >= 1 && theMonth <= 12) { month = theMonth; return true; } else return false; } }

9 SE15: Exceptions27–9 Dealing With Returned Value do { String input = JOptionPane.showInputDialog(...); int month = Integer.parseInt(input); } while (! date.setMonth(month)); String input = JOptionPane.showInputDialog(...); int month = Integer.parseInt(input); date.setMonth(month);... We should do something like this: …but we are allowed to do this:

10 SE15: Exceptions27–10 Problems Class author May not be able to return a boolean from a method Class user Other code may ‘swallow up’ the return value

11 SE15: Exceptions27–11 The Solution: Exceptions Build on the idea of transferring error-handling responsibility to a context better able to deal with it Not the same as returning an error value Exceptions change flow of control Exceptions cannot be ignored Two aspects Signalling the error by throwing an exception Dealing with the error by catching the exception

12 SE15: Exceptions27–12 Exceptions in Java Exceptions are objects, containing Variables to provide information on the error Methods to access that information Two types Unchecked ArrayIndexOutOfBoundsException NullPointerException NumberFormatException Checked FileNotFoundException

13 SE15: Exceptions27–13 Flow of Control After an exception is thrown, normal flow of program execution is suspended JVM looks for an exception handler in the method If there is no handler in the method, the exception moves up to the caller of the method… Eventually, exception can end up in main If main doesn’t handle it, the program is halted

14 SE15: Exceptions27–14 Flow of Control public static void main(String[] args) { foo(); } public static void foo() { bar(); System.out.println("Foo!"); } public static void bar() { int impossibleResult = 1/0; System.out.println(impossibleResult); } ArithmeticException occurs here… …and propagates to here… …then to here, where it halts program These don’t execute!

15 SE15: Exceptions27–15 Dealing With Exceptions Put code that might throw an exception in a try block Add one or more catch blocks to match the types of exception that could be thrown Write code inside the catch blocks Print an error message Log details of error to a file Fix the problem! Never ‘swallow’ exceptions with empty catch blocks!

16 SE15: Exceptions27–16 Example try { Scanner keyboard = new Scanner(System.in); System.out.print("Enter numerator: "); int n = keyboard.nextInt(); System.out.print("Enter denominator: "); int d = keyboard.nextInt(); int result = n/d; System.out.println("Result = " + result); } catch (ArithmeticException error) { System.err.println("Division by zero!"); }

17 SE15: Exceptions27–17 Catching Exceptions in main Often a good idea Gives us control of how the program terminates public static void main(String[] args) { try {... } catch (Exception error) { error.printStackTrace(); //System.err.println(error); System.exit(1); } } Intercepts everything! Prints full details Prints simple error message

18 SE15: Exceptions27–18 Summary We have Seen that traditional approaches to error handling are either too inflexible or too easily ignored Considered how exceptions can solve these problems Examined how flow of control within a program changes when an exception is thrown Looked at examples of Java exceptions Discussed how to intercept exceptions in Java programs


Download ppt "Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming."

Similar presentations


Ads by Google