Professional Documents
Culture Documents
Chapter Six Oop
Chapter Six Oop
Exceptions
6.1 Understanding Exceptions in Java
An exception is an error that happens when an application is running.
Exceptions are events that occur during the execution of programs that disrupt the normal flow of
instructions.
In Java, an exception is an object that wraps an error event that occurred within a method and
contains:
Information about the error including its type
The state of the program when the error occurred
Optionally, other custom information
When an exception condition happens, the exception is said to be thrown.
All exceptions are represented by classes organized into a hierarchy tree
The Throwable class, which is the root of the exceptions tree, is the base class of all the
exceptions in Java.
The two subclasses of Throwable, directly derived from Throwable, are Error and Exception.
Errors (represented by subclasses of Error) occur in the Java virtual machine (JVM) and not in the
application itself. Error generally represents an unusual problem from which it is difficult to
recover.
The exceptions (represented by subclasses of Exception), on the other hand, generally originate
from within the application.it occurs do to programming error or system error.
The exceptions should generally be handled in the program, but you are not required to handle all
of them. Some examples of exceptions are division by zero, out of boundary arrays, and file
input/output problems.
Exception class can be grouped into two main categories: checked exceptions and unchecked
exceptions.
Checked Exceptions:
This is the category of exceptions for which the compiler checks to ensure that your code is
prepared for them.” prepare for unwelcome but expected guests”
These exceptions are the instances of the Exception class or one of its subclasses, excluding the
Runtime Exception sub tree.
Checked exceptions are generally related to how the program interacts with its environment. For
example, URISyntaxException and ClassNotFoundException are checked exceptions.
The conditions that generate checked exceptions are generally outside the control of your
program, and hence they can occur in a correct program.
However, you can anticipate (expect) them, and thus you must write the code to deal with them.
The rule is: when checked exceptions are expected, either declare it in the throws clause of your
method or catch it in the body of your method, or do both.
Note: The programmer is required to write code to deal with checked exceptions. The compiler
checks that such code exists. As opposed to the checked exceptions, the runtime exceptions are
not checked by the compiler.
The suspected code is embraced in the try block, followed by the catch block in which the code to
handle the exception is written.
An exception of type ArithmeticException would be thrown due to line 6 (division by zero), and this
exception would be caught at line 9.The variable ae is an object reference to the instance of
ArithmeticException, and contains the message regarding the exception.
Note that the execution control jumps from line 6 directly to line 9. The execution continues after the last
catch block as if nothing happened. Therefore, the output is:
If an exception is thrown from the try block and an appropriate catch block (to catch the exception) did
not exist, the method would be terminated without executing any other line from the method.
Note: A catch block is executed only if it catches the exception coming from within the
corresponding try block.
It is possible to have multiple catch blocks, in which case the execution control will jump to the first
line immediately following the last catch block, after executing the right (matching) catch block.
if no exception arises from within the try block, all the catch blocks are skipped.
e.g. ... // Compiler complains catch blocks at (1) and (2), which
catch (Exception e) { // (1) superclass will result in a compile time error:
System.out.println(e); the superclass Exception will
} catch (ArithmeticException e) { //(2) subclass shadow the subclass
System.out.println(e); ArithmeticException.
} ...
<statements> }
<statements> }
...
<statements> }
<statements> }
Example :
class ExmpleMB{
public static void main(String args[]) {
try {
int a = 0; //check it with non zero
System.out.println("a = " + a);
int b = 20/ a;
int c[] = { 2 };
c[20] = 99;
} catch(ArithmeticException e) {
System.out.println("Divide by 0: " + e);
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array index oob: " + e);
}
finally {
System.out.println(“finally always printed whether or not an exception is thrown “);
}