150 likes | 158 Views
This lecture provides an introduction to exceptions in Java, including exception handling, try-catch blocks, throw statements, and creating custom exception classes.
E N D
Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13th, 2005
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 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11
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 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11
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? CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11
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 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11
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 } CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11
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 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11
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 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11
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 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11
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 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 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 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11
Methods available to Exceptions • getMessage() • Ex. e.getMessage(); • printStackTrace() • toString() • getLocalizedMessage() • fillInStackTrace() CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11
How to Create Your Own Exception Classes See Ex. AgeExceptionThe class hierarchy for the AgeException classes Exception AgeException OutOfAgeLimitException IllegalAgeFormatException NegativeAgeException TooYoungException TooOldException CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11
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 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11
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 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 11