Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005.

Similar presentations


Presentation on theme: "Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005."— Presentation transcript:

1 Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005

2 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11 2 Outline Intro. To Exceptions in Java Java Exception Handling Try, catch, finally blocks throw statement and throws clause How to create your own exception classes Ex. AgeException classes Exceptions in GUI

3 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11 3 Intro. to Exceptions in Java Run-time errors happen User enters incorrect input Resource is not available (ex. file) Logic error (bug) that was not fixed For Production software Having a program "crash" is a HIGHLY UNDESIRABLE thing Users think software is no good Lose confidence

4 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11 4 Intro. to Exceptions in Java Exception: An occurrence of an erroneous, unusual or unexpected event in a program execution In older languages Code the handling of exceptions into each area of the program that needed it Some exceptions could not even be handled by the HLL  ex. standard Pascal cannot handle I/O errors or division by 0  Ask for integer and user enters a text string – what do you do?

5 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11 5 Intro. to Exceptions in Java In newer languages Exception handling built into the language We can separate exception handling from the "main line" code Java uses the same exception handling model used in C++ Exceptions are objects that are thrown and catched Some exceptions are built into the language Others can be created and thrown by the programmer

6 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11 6 Exceptions in Java Java exception handling Exceptions are handled using try-catch blocks try { // code that will normally execute } catch (ExceptionType1 e) { // code to "handle" this exception } catch (ExceptionType2 e) { // code to "handle" this exception }... // can have many catches finally { // code to "clean up" before leaving try block }

7 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11 7 Exceptions in Java If all goes well (no exceptions occur) Code in try block is executed, followed by code in (optional) finally block If an exception occurs anywhere in the try block Execution immediately jumps out of the try block An exception handler is sought in a catch block If exception is handled in a catch block, that block executes; if not, exception is propagated Whether exception is handled or propagated, finally block is executed

8 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11 8 Exceptions in Java If an exception is handled Execution resumes immediately AFTER try/catch block in which it was handled, and does NOT return to throw point termination model of exception handling  As opposed to a resumption model, where execution resumes from where the exception occurred If an exception is propagated A handler is searched for by backing up through the call chain on the run-time stack This is dynamic exception propagation If no handler is ever found  Console applications crash and report exception  GUI applications will continue to execute, but may be in an inconsistent state – more soon

9 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11 9 Exceptions in Java Checked vs. Unchecked exceptions Checked exceptions If a method does NOT handle these, the method MUST state that it throws them  Done in a throws clause in the method header These include IOException, and InterruptedException (and their subclasses) Unchecked exceptions Method not required to explicitly "throw" these These include RunTimeException and Error

10 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11 10 Exceptions in Java Catching exceptions Catching a superclass of an exception will catch subclass exception objects catch (Exception e)  "catch all" if no other exceptions match Should list exceptions in order of most specific to most general  If catch above is first NO OTHER catches in the block could ever execute It is better style to be as specific as possible with the exceptions that are caught See ex26.java

11 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11 11 throw statement & throws clause throw statement Causes an exception to be thrown. throws clause A method that throws an exception within it must catch that exception or have that exception declared in its throws clause When multiple exceptions are to be put in one throws clause, use commas to separate them int readModel(String filename) throws IOException, InterruptedException

12 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11 12 Methods available to Exceptions getMessage() Ex. e.getMessage(); printStackTrace() toString() getLocalizedMessage() fillInStackTrace()

13 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11 13 See Ex. AgeException The class hierarchy for the AgeException classes Exception AgeException IllegalAgeFormatExceptionOutOfAgeLimitException NegativeAgeException TooOldExceptionTooYoungException How to Create Your Own Exception Classes

14 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11 14 Exceptions in GUIs GUIs run using multiple execution threads A thread is a logically separate execution chain that shares the same data See board Events in GUIs are generated and handled by threads In future courses you will see how to use threads yourselves For now we just want to know the effect of exceptions on applications that have multiple threads

15 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11 15 Exceptions in GUIs If the thread in which the exception was thrown does not handle it, the thread will terminate However, other threads will continue the execute, so GUI may continue to run This does NOT mean that it will run correctly The exception may have caused a problem that persists in the GUI Don't think that because the window didn't close that everything is ok It is best to always try to anticipate and handle exceptions in GUIs See MiniCalcTwo.java, DoMathInt.java, DoMathIntCheck.java


Download ppt "Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005."

Similar presentations


Ads by Google