320 likes | 490 Views
8/13/2012. Komar Associates. 2. Overview. When to use exception handlingJava Exception Handlingtry blocksThrowing and catching exceptionsConstructors, finalizers and exception handlingfinally blockprintStackTrace and getMessage. 8/13/2012. Komar Associates. 3. Exception Handling. Similar to th
E N D
1. 8/13/2012 Komar Associates 1 Exception Handling Joe Komar
2. 8/13/2012 Komar Associates 2 Overview When to use exception handling
Java Exception Handling
try blocks
Throwing and catching exceptions
Constructors, finalizers and exception handling
finally block
printStackTrace and getMessage
3. 8/13/2012 Komar Associates 3 Exception Handling Similar to the established C++ methodology
Synchronous exceptions (divide by zero) versus asynchronous exceptions (interrupts)
Improves a programs fault tolerance
Build in from the beginning
4. 8/13/2012 Komar Associates 4 Exceptions Exception -- thrown by program or runtime environment and can be caught and handled
Error -- similar to an exception but represents an unrecoverable situation and should not be handled
Java:
Exception and Error classes derived from the Throwable class
Specific exceptions and errors derived from their classes Pages 666-668 for diagram of how the inheritance hierarchy goes for exceptions and errors
In real world, need to handle exceptions otherwise program bombs -- a lot of programmer time spent on this.Pages 666-668 for diagram of how the inheritance hierarchy goes for exceptions and errors
In real world, need to handle exceptions otherwise program bombs -- a lot of programmer time spent on this.
5. 8/13/2012 Komar Associates 5 Exception Execution Flow Distinguish Exception flow from Normal flow
Debugging is made easier (pinpoint errors)
Java API classes often throw exceptions, letting the programmer decide what to do with them
Normal flow is typically 20% of the total code
Some programs should never abnormally abort
Deal with exceptions by:
choosing not to handle them (abnormal abort)
handle the exception where it occurs
handle the exception at another point (throws) Separate the normal flow from the exception flow makes for better designed and more readable programsSeparate the normal flow from the exception flow makes for better designed and more readable programs
6. 8/13/2012 Komar Associates 6 Use Exception Handling To.. process where method is unable to complete its task
process where components arent geared to handle them
process where components are widely used and cannot handle themselves
on large projects to handle exceptions in a uniform way
7. 8/13/2012 Komar Associates 7 The try Statement The try statement identifies a block of statements that may throw an exception There can be a number of catch blocks following the try block that can process different types of errors