Exception Handling
(Notes from Java How to Program, Fifth Edition by Harvey M. Deitel, Paul J. Deitel )
An exception is an indication that a problem has occurred in the program's execution.
Why is exception handling important?
Exception handling is used in situations in which the system can recover from the malfunction that caused the exception. The recovery process is called the exception handler.
Using Java's standardized exception handling rather than having programmers use a diversity of "home-grown" techniques improves program clarity on large projects.
Use exceptions for malfunctions that must be processed in a different method from where they are detected. Use conventional error-handling techniques for local error processing in which a method is able to deal with its own exceptions.
Exception handling is particularly well suited to systems of separately developed components. Such systems are typical of real-world software. Exception handling makes it easier to combine components and have them work together effectively.
Without exception handling programmers often delay in writing error-processing code, or forget to include it. This results in less robust systems. Java forces the programmer to deal with exceptions from the start of the project. It is still a lot of work. But it is much more difficult to add exception handling as an afterthought.
Where to deal with errors?
Usual way is interspersed within the code, dealing with them as they may occur. The problem is the code becomes "polluted" with error processing. This makes the code difficult to read.
Using Java exception handling enables the programmer to remove error-handling code from the "main line" of the program execution. This improves program clarity and enhances modifiability.
Common Exceptions
Key Words
try
throw
catch
finally
try {
statements
resource-acquisition statements
}
catch {AKindOfException exception1) {
exception-handling statements
}
catch {AnotherKindOfException exception2) {
exception-handling statements
}
finally {
statements
resource-release statements
}
Good Programming Practice
Do not place try/catch/finally around every statement that may throw an exception. Rather place one try block around a significant portion of your code, followed by catch blocks that handle those exceptions. Followed by a single finally block, if necessary.
Exception Handling
Ben Peacock & Aleem Abdulla
Exception handling is used in Java to allow programs to keep running, even after a significant error has occurred. A number of pre-defined errors exist in Java, and new error objects can be created by using inheritance:
class ExitException extends Exception{}
Here is an exception handling example (with new keywords in bold)
try
{
// this is where your program code is, where the errors occur, and where // exceptions are thrown
throw new exception_name();
}
catch(exception_name x)
{
// this is where you put code that you want to execute when the error, // exception_name occurs
system.out.println("An exception occurred");
}
finally
{
//the code here is always executed, unless you prematurely end the //program
system.out.println("The program is over");
}
Tutorial on exceptions http://www.joegrip.com/seeit.html
Assignment 4: Create a program that asks the user to input a number between one and ten. Use exception handling to determine if the number is too high or too low or not a number at all. Use three catch blocks.