Exception Handling by Manipal Techh
Exception Handling by Manipal Techh
HANDLING
1
Exception Types
All exception types are subclasses of the
built-in class Throwable
Immediately below Throwable are two
subclasses that partition exceptions into two
distinct branches
One branch is headed by Exception
This class is used for exceptional conditions
that user programs should catch
6
Uncaught Exceptions
class Exc0 {
public static void main(String args[]) {
int d = 0;
int a = 42 / d;
}
}
Java run-time constructs a new exception object
and then throws this exception. This causes the
execution of Exc0 to stop, because once an
exception has been thrown, it must be caught by
an exception handler and dealt with immediately.
The default handler displays a string describing
the exception, prints a stack trace from the point
at which the exception occurred, and terminates
the program.
class Exc2 {
public static void main(String args[]) {
int d, a;
try {
// monitor a block of code.
d = 0;
a = 42 / d;
System.out.println("This will not be printed.");
} catch (ArithmeticException e) { // catch divideby-zero error
System.out.println("Division by zero."); }
System.out.println("After catch statement."); }
}
This program generates the following output:
Division by zero.
After catch statement.
12
14
catch (ArithmeticException e) {
System.out.println("Exception: " + e);
a = 0; // set a to zero and continue
}
Exception: java.lang.ArithmeticException: / by
zero
15
throw:
It is possible for your program to throw an
exception
explicitly,
using
the
throw
statement
The general form of throw is shown here:
throw ThrowableInstance;
23
class ThrowDemo {
// Demonstrate
throw.
static void demoproc() {
try {
throw new NullPointerException("demo");
} catch(NullPointerException e) {
System.out.println("Caught inside
demoproc.");
throw e; // rethrow the exception
}
}
public static void main(String args[]) {
try {
demoproc();
} catch(NullPointerException e) {
System.out.println("Recaught: " + e);
}}}
25
throws
Output:
Caught inside demoproc.
Recaught: java.lang.NullPointerException: demo
26
class ThrowsDemo {
static void throwOne() throws IllegalAccessException {
System.out.println("Inside throwOne.");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
try {
throwOne();
} catch (IllegalAccessException e) {
System.out.println("Caught " + e);
}
}
}
Here is the output generated by running this example
program:
inside throwOne
caught java.lang.IllegalAccessException: demo
29
finally
finally creates a block of code that will be
executed after a try/catch block has
completed and before the code following the
try/catch block
The finally block will execute whether or not
an exception is thrown
If an exception is thrown, the finally block
will execute even if no catch statement
matches the exception
30
31
// Demonstrate finally.
class FinallyDemo {
// Through an exception out of the method.
static void procA() {
try {
System.out.println("inside procA");
throw new RuntimeException("demo");
} finally {
System.out.println("procA's finally");
}
}
// Return from within a try block.
static void procB() {
try {
System.out.println("inside procB");
return;
} finally {
System.out.println("procB's finally");
}
}
32
33
35
36
37