Exception HandlingMotivationOverviewSlide 4Exception HandlerExampleSequence of Events for throwSequence of Events for No throwJava Exception HierarchyInheritance hierarchy for class ThrowableHandling ExceptionsUsing finallySequence of Events for finally clauseStack UnwindingprintStackTrace, getStackTrace and getMessageChained ExceptionsException HandlingChapter 132Motivation•We seek programs•When something unexpected occurs–Ensure program ________________________ the problem–Then program must do something about it•Extensive testing of special situations can result in "____________________ code"•Need mechanism to check for problem where it could occur•When condition does occur–Have _________________ to code to handle the problem3Overview•Exception–Indication of ___________________ during execution•Uses of exception handling–Process exceptions from program components–Handle exceptions in a uniform manner ______________–Remove ___________ code from “main line” of execution •A method detects an error and throws an exception–Exception handler processes the error–Uncaught exceptions yield _____________ effects •Might terminate program execution4Overview•Code that could generate errors put in ________ blocks –Code for error handling enclosed in a __________ clause–The ____________ clause always executes•Termination model of exception handling–The block in which the exception occurs expires•throws clause specifies _____________ method throws5Exception HandlerException "thrown" hereException handlerException handlerThrown exception matched against first set of exception handlersIf it fails to match, it is matched against set of handlers, etc.If exception matches ___________ of handlers, program is abandoned6Example•Consider the problem of division by zero•Note elements of Figure 13.1–try block–Following catch blocks–throws specification7Sequence of Events for throwPreceding steptry blockthrow statementunmatched catchmatching catchunmatched catchnext step8Sequence of Events for No throwPreceding steptry blockthrow statementunmatched catchmatching catchunmatched catchnext step9Java Exception Hierarchy•__________________ Throwable–Subclass ________________•Exceptional situations•Should be caught by program–Subclass ______________•Typically not caught by program•Checked exceptions–Catch or _________________•Unchecked exceptions10Inheritance hierarchy for class ThrowableThrowableException ErrorAWTError ThreadDeathIOExceptionRuntimeException OutOfMemoryError11Handling Exceptions•Rethrow exception if catch cannot handle it•Resource leak–Caused when _____________________________ by a program•The finally block–Appears after catch blocks–Always ________________–Optional–Use to release resources12Using finally•View program, Figure 13.3•Note–__________________ of exception–Code for throw exception–Blocks using ________________ •Suggestion–Do not use a try block for ____________________ which may cause a problem–Enclose _____________________ of statements–Follow by _______________ catch blocks13Sequence of Events for finally clausePreceding steptry blockthrow statementunmatched catchmatching catchunmatched catchnext stepfinally14Stack Unwinding•Exception not caught in scope–Method _________________–Stack ______________________ occurs–Another attempt to catch exception•View Figure 13.4•Note–Sequence of events during run–Resulting output15printStackTrace, getStackTrace and getMessage•Throwable class–Method printStackTrace•Prints method call __________________–Method getStackTrace •Obtains stack-trace information–Method getMessage•Returns _____________________ string•View example of these, Figure 13.516Chained Exceptions•Useful for a catch handler to–Catch one exception type–Then throw a new exception of ________________–Indicates program-specific exception•Chained exceptions enable–Exception object to ____________________ info•Figure 13.6 demonstrates use of chained
View Full Document