180 likes | 190 Views
SE-1021 Software Engineering II. Week 6, Day 1 Exceptions GUIs and Files Tomorrow Demo Lab 5 Demo first milestone for Lab 6. Exception Handling Terms. Exception – An object thrown when a problem occurs
E N D
SE-1021Software Engineering II • Week 6, Day 1 • Exceptions • GUIs and Files • Tomorrow • Demo Lab 5 • Demo first milestone for Lab 6 SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick
Exception Handling Terms Exception – An object thrown when a problem occurs Throw – Unwind the stack looking for a method that called this one that can fix the problem. Stack – A list of the method that called this one, and the method that called the method that called this one, etc. Stack Trace – Printout of the current stack SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick
Exception Handling Terms Handle an exception – Taking care of the problem 1. “pass the buck” Pass it on to the caller of this method. 2. Catch the exception. Catch – Stop unwinding the stack and clean up after the problem SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick
(footnote) • “pass the buck” means give someone else responsibility for handling a problem • President Truman had a “Buck Stops Here” sign indicating he “caught” the exceptions… SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick http://www.trumanlibrary.org/buckstop.htm
More Definitions • Checked exception – An exception that the compiler checks is handled • Unchecked exception – An exception that the compiler implicitly “throws” from a method if it is not handled explicitly. • Error – An object similar to an exception which should never be caught. This indicates a problem that is (probably) impossible to recover from. [See past code examples] SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick
The Throwable Hierarchy SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick
Legend… • Children of Error are “Errors” (e.g. A,B) • Children of Exception are “Checked Exceptions” (e.g. C,D) • Children of RuntimeException are “Unchecked Exceptions” (e.g. E,F) SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick
Exercise: Which are checked, unchecked? Which should not be caught? SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick
General Principle for Handling Exceptions • If a problem can be handled simply without exceptions, don’t use them • Otherwise, do use them • [See our first program with exceptions for an example:https://faculty-web.msoe.edu/yoder/se1021/code/class4_3_ExceptionsWithCookieDivider/] SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick
Multiple try/catch blocks • A try/catch statement can have multiple catch blocks • An exception may match multiple catch blocks – any parent class that the specific exception extends • Only one catch block will catch the exception SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick
Finally • In a try-catch-finally statement, the finally statement is for cleanup. • It always happens* • Even if no catch clause catches the exception [Show example FinallyTest here] *unless the power is unplugged or you put in an infinite loop… SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick
To catch individually, what order? • Exception • IllegalArgumentException • IOException • NumberFormatException • RuntimeException SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick
… } catch ( ) { } catch ( ) { } catch ( ) { } catch ( ) { } catch ( ) { } SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick
For 2017-2018 Offering [We already discussed this this year. See, https://faculty-web.msoe.edu/yoder/se1021/code/ e.g., https://faculty-web.msoe.edu/yoder/se1021/code/class5_1_ExceptionsWithSimplifiedCookieDivider/] SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick
public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please enter two integers: "); try { int numer = in.nextInt(); System.out.println("n: "+numer); //in.close(); int denom = in.nextInt(); System.out.println("d: "+denom); int ratio = numer/denom; … SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick
… } catch (InputMismatchException e) { System.out.println(“ime"); } catch (Exception e) { System.out.println(“e"); } finally { System.out.println("f"); } System.out.println("done"); } SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick
In the code on the previous slides • What is printed if • The user enters “5 2” • The user enters “5 asdf” • How about? … • The user enters “5 0” • We uncomment that line that closes the input and enter “5 2” (See the exceptions that in.nextInt throws) • We comment out the catch(Exception e) block and enter “5 0”? SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick
Code from last time • How to fix it so we don’t get 2.0 for 5/2? • How to fix it so once we have succeeded in computing a ratio, we exit? • We still want to exit if the user hits cancel or enters “q” • [We did not do this exercise in class… can you do it on your own?] SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick