Download Exception Handling - Java and the Web - Lecture Slides and more Slides Java Programming in PDF only on Docsity!
Exception Handling
What Is an Exception?
- Many developers are misguided by the term exception handling. These developers believe that the word exception is related to how frequently something happens.
- For example, a developer designing a file Read method is likely to say the following: “ When reading from a file, you will eventually reach the end of its data. Since reaching the end will always happen, I’ll design my Read method so that it reports the end by returning a special value; I won’t have it throw an exception.”
What Exactly Is an Exception?
- An exception means that an action member cannot complete the task it was supposed to perform as indicated by its name.
- Look at the following class definition: class Account { public static void Transfer(Account from, Account to, float amount) {... } }
A Meaningful Exception
- The Transfer method accepts two Account objects and a float
value that identifiers an amount of money to transfer between accounts. The Transfer method could failed for many reasons, like from or to argument might be null.
- When the Transfer method is called, its code must check for
all the failed possibilities, and if any of which is detected, it cannot transfer the money and should notify the caller that it failed by throwing an exception.
Don’t Catch Everything
- A ubiquitous mistake made by developers who have not been
properly trained on the proper use of exceptions is to use catch blocks too frequently and often improperly.
- When you catch an exception, you’re stating that you
expected this exception, you understand why it occurred, and you know how to deal with it. In other words, you’re defining a policy for the application.
Don’t Catch Everything (Cont’)
• This code indicates that it was expecting any
and all exceptions and knows how to recover
from any and all situations.
try { //try to execute code that the programmer //knows might fail.. } catch (exception) {
}
Don’t Catch Everything (Cont’)
- In addition, it is possible that an exception was thrown
because some object was in a bad state. If library code catches and swallows the exception, the program continues running with unpredictable results and with potential security vulnerabilities.
- It is better for the exception to be unhandled and for the
application to terminate.
Don’t Catch Everything (Cont’)
- In fact, most unhandled exceptions will be discovered during testing of your code. To fix these unhandled exception, you will either modify the code to look for specific exception, or you will rewrite the code to eliminate the conditions that cause the exception to be thrown.
- The final version of the code that will be running in a production environment should see very few (if any) unhandled exceptions and be extremely robust.
Exception Hierarchy
Errors
• Errors are events generated by the JVM that
have had a critical and fatal impact on the
running application.
• They are typically not handled by regular
programs
Java built-in exceptions
• ArithmeticException
• ArrayIndexOutOfBounds
• ArrayStore
• ClassCast
• IllegalArgument
• IllegalState
• IllegalThreadState
Checked Exceptions
• Nonruntime ( checked exceptions ) :
- These exceptions occur outside of the runtime system
- Input / Output exceptions
- Caught or specified by the system compiler
Handling Exceptions
try {
} catch (ExceptionType) {
} catch (ExceptionType) {
} finally {
}
Multiple catch statements
• Once a try statement has been used to
enclose a code segment, the catch can be
used to handle one or more specific exception
types. By defining catch statements for
specific exception types, a more accurate
handling of the exception can be tailored to
the programs needs