Java Reference
In-Depth Information
Notice that when you added error handling to your four lines of pseudocode, the code bloated to over twenty
lines. The worst thing about the above code is that the code that performs the action has been cluttered with error-
handling code. It has also introduced many nested if-else statements resulting in spaghetti code.
In the last two examples, you saw that the way of handling errors that uses if-else statements is not elegant
and maintainable. Java has a better way to handle errors: by separating the code that performs actions from the code
that handles errors. In Java, we use the phrase “exception” instead of “error” to indicate an abnormal condition in a
program; the phrase “exception handling” is used instead of the phrase “error handling.” In general, we say that an error
occurs and you handle it. In Java, we say that an exception is thrown and you catch it. This is the reason that exception
handling is also called catching an exception . The code that handles the exception is known as an exception handler .
You could rewrite the above pseudocode using Java syntax (not full-fledged Java code, though) as follows:
try {
// Connect to the database
// Fetch employee record
// Update employee salary
// Commit the changes
}
catch(DbConnectionException e1){
// Handle DB Connection exception here
}
catch(EmpNotFoundException e2){
// Handle employee not found exception here
}
catch(UpdateFailedException e3){
// Handle update failed exception here
}
catch(CommitFailedException e4){
// Handle commit failed exception here
}
You do not need to understand the above pseudocode fully. I will discuss the details shortly. You need to observe
the structure of the code, which allows for separation of the code that performs actions from the code that handles
exceptions. The code that performs the actions is placed inside a try block and the code that handles the exception
is placed inside a catch block. You will observe that this code is much better in terms of elegance and maintainability
compared to the previous attempt in which you had to write many if-else statements to achieve the same.
in Java, an exception is thrown and caught. Catching an exception is the same as handling the exception.
the code that performs the action may throw an exception and the code that handles the exception catches the thrown
exception. this style of exception handling allows you to separate the code that performs actions from the code that
handles the exceptions that may arise while performing the actions.
Tip
An Exception Is an Object
How does the exception handling part of the code know about the exception that occurs in another part of the code?
When an exception occurs, Java creates an object with all pieces of information about the exception (e.g., type of
exception, line number in the code where the exception occurred, etc.) and passes it to the appropriate exception
handling code. The term “exception” is used to mean one of two things—the exceptional condition and the Java object
to represent the exceptional condition. The meaning of the term will be clear from the context.
 
 
Search WWH ::




Custom Search