Monday, 4 February 2013

EXCEPTION HANDLING | Java J2EE Tutorial pdf

EXCEPTION HANDLING

EXCEPTIONS
A Java program should never "core dump," no matter how buggy it is. If the compiler excepts it and something goes wrong at run time, Java throws an exception. By default, an exception causes the program to terminate with an error message, but we can also catch an exception.
try {
// ...
foo.bar();
// ...
a[i] = 17;
// ...
}
catch (IndexOutOfBoundsException e) {err.println("Oops: " + e); }

The try statement says we're interested in catching exceptions. The catch clause (which can only appear after a try) says what to do if an IndexOutOfBoundsException occurs anywhere in the try clause. In this case, we print an error message. The toString() method of an exception generates a string containing information about what went wrong, as well as a call trace.
WARNING Never write an empty catch clause. If we do, we will regret it. Maybe not today, but tomorrow and for the rest of our life.
Because we caught this exception, it will not terminate the program. If some other kind of exception occurs (such as divide by zero), the exception will be thrown back to the caller of this function and if that function doesn't catch it, it will be thrown to that function's caller, and so on back to the main function, where it will terminate the program if it isn't caught. Similarly, if the function foo.bar throws an IndexOutOfBoundsException and doesn't catch it, we will catch it here.
The catch clause actually catches IndexOutOfBoundsException or any of its subclasses, including ArrayIndexOutOfBoundsException , StringIndexOutOfBoundsException , and others.
An Exception is just another kind of object, and the same rules for inheritance hold for exceptions as any other king of class.
We can define and throw our own exceptions.
class SytaxError extends Exception {
int lineNumber;
SytaxError(String reason, int line) {
super(reason);
lineNumber = line;
}
public String toString() {
return "Syntax error on line " + lineNumber + ": " + getMessage();
}
}
class SomeOtherClass {
public void parse(String line) throws SyntaxError {
// ...
if (...)
throw new SyntaxError("missing comma", currentLine);
//...
}
public void parseFile(String fname) {
//...
try {
// ...
nextLine = in.readLine();
parse(nextLine);
// ...
}
catch (SyntaxError e) {
err.println(e);
}
}}

Each function must declare in its header (with the keyword throws) all the exceptions that may be thrown by it or any function it calls. It doesn't have to declare exceptions it catches. Some exceptions, such as IndexOutOfBoundsException, are so common that Java makes an exception for them (sorry about that pun) and doesn't require that they be declared. This rule applies to RuntimeException and its subclasses. We should never define new subclasses of RuntimeException.
There can be several catch clauses at the end of a try statement, to catch various kinds of exceptions. The first one that "matches" the exception (i.e., is a super class of it) is executed. We can also add a finally clause, which will always be executed, no matter how the program leaves the try clause (whether by falling through the bottom, executing a return, break, or continue, or throwing an exception).

No comments: