1 / 18

SE-1021 Software Engineering II

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

bscully
Download Presentation

SE-1021 Software Engineering II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. (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

  5. 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

  6. The Throwable Hierarchy SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick

  7. 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

  8. Exercise: Which are checked, unchecked? Which should not be caught? SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick

  9. 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

  10. 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

  11. 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

  12. To catch individually, what order? • Exception • IllegalArgumentException • IOException • NumberFormatException • RuntimeException SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick

  13. } catch ( ) { } catch ( ) { } catch ( ) { } catch ( ) { } catch ( ) { } SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick

  14. 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

  15. 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

  16. } 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

  17. 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

  18. 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

More Related