Lec 11 Chapter 11 Exception Jiang Jen ZHENG

  • Slides: 15
Download presentation
Lec. 11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005 CS 401/COE

Lec. 11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005 CS 401/COE 401 Summer 2005. Department of Computer Science. University of Pittsburgh. Lecture 11

Outline n Intro. To Exceptions in Java Exception Handling n Try, catch, finally blocks

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

Intro. to Exceptions in Java n Run-time errors happen n User enters incorrect input

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

Intro. to Exceptions in Java n Exception: n An occurrence of an erroneous, unusual

Intro. to Exceptions in Java n Exception: n An occurrence of an erroneous, unusual or unexpected event in a program execution n In older languages n n 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? CS 401/COE 401 Summer 2005. Department of Computer Science. University of Pittsburgh. Lecture 11 4

Intro. to Exceptions in Java n In newer languages n n n Exception handling

Intro. to Exceptions in Java n In newer languages n n n 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 CS 401/COE 401 Summer 2005. Department of Computer Science. University of Pittsburgh. Lecture 11 5

Exceptions in Java exception handling n Exceptions are handled using try-catch blocks try {

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

Exceptions in Java n If all goes well (no exceptions occur) n n If

Exceptions in Java n If all goes well (no exceptions occur) n n If an exception occurs anywhere in the try block n n Code in try block is executed, followed by code in (optional) finally 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 CS 401/COE 401 Summer 2005. Department of Computer Science. University of Pittsburgh. Lecture 11 7

Exceptions in Java n If an exception is handled n n Execution resumes immediately

Exceptions in Java n If an exception is handled n n 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 n If an exception is propagated n n n 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 continue toof execute, but may be 8 in CS 401/COE 401 will Summer 2005. Department Computer Science. University of an inconsistent state – more soon Pittsburgh. Lecture 11

Exceptions in Java n Checked vs. Unchecked exceptions n Checked exceptions n If a

Exceptions in Java n Checked vs. Unchecked exceptions n Checked exceptions n If a method does NOT handle these, the method MUST state that it throws them § Done in a throws clause in the method header n n These include IOException, and Interrupted. Exception (and their subclasses) Unchecked exceptions n n Method not required to explicitly "throw" these These include Run. Time. Exception and Error CS 401/COE 401 Summer 2005. Department of Computer Science. University of Pittsburgh. Lecture 11 9

Exceptions in Java n Catching exceptions n Catching a superclass of an exception will

Exceptions in Java n Catching exceptions n Catching a superclass of an exception will catch subclass exception objects catch (Exception e) § "catch all" if no other exceptions match n 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 n It is better style to be as specific as possible with the exceptions that are caught n See ex 26. java CS 401/COE 401 Summer 2005. Department of Computer Science. University of Pittsburgh. Lecture 11 10

throw statement & throws clause n throw statement n Causes an exception to be

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

Methods available to Exceptions n get. Message() n Ex. e. get. Message(); n print.

Methods available to Exceptions n get. Message() n Ex. e. get. Message(); n print. Stack. Trace() n to. String() n get. Localized. Message() n fill. In. Stack. Trace() CS 401/COE 401 Summer 2005. Department of Computer Science. University of Pittsburgh. Lecture 11 12

How to Create Your Own Exception Classes See Ex. Age. Exception The class hierarchy

How to Create Your Own Exception Classes See Ex. Age. Exception The class hierarchy for the Age. Exception classes Exception Age. Exception Out. Of. Age. Limit. Exception Illegal. Age. Format. Exception Negative. Age. Exception Too. Young. Exception Too. Old. Exception CS 401/COE 401 Summer 2005. Department of Computer Science. University of Pittsburgh. Lecture 11 13

Exceptions in GUIs run using multiple execution threads n A thread is a logically

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

Exceptions in GUIs n If the thread in which the exception was thrown does

Exceptions in GUIs n If the thread in which the exception was thrown does not handle it, the thread will terminate n n This does NOT mean that it will run correctly n n n However, other threads will continue the execute, so GUI may continue to run 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 n See Mini. Calc. Two. java, Do. Math. Int. Check. java CS 401/COE 401 Summer 2005. Department of Computer Science. University of Pittsburgh. Lecture 11 15