Monday, September 13, 2010

Object Oriented Programming (OOP) Sep 13th

Exception Handling
================

EXCEPTION TYPES
===============


All exception types are subclasses of the built-in class Throwable.

Thus, Throwable is at the top of the exception class hierarchy.

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. This is also the class that you will subclass to create your own custom exception types.

There is an important subclass of Exception, called RuntimeException.

Exceptions of this type are automatically defined for the programs that you write
and include things such as :


  • division by zero
  • invalid array indexing.
The other branch is topped by Error, which defines exceptions that are not expected
to be caught under normal circumstances by your program. Exceptions of type Error
are used by the Java run-time system to indicate errors having to do with the run-time
environment, itself.

  • Stack overflow is an example of such an error. This chapter will not be dealing with exceptions of type Error, because these are typically created in response to catastrophic failures that cannot usually be handled by your program.

Uncaught Exceptions
=================

program includes an expression that
intentionally causes a divide-by-zero error


class Exc0 {
public static void main(String args[]) {
int d = 0;
int a = 42 / d;
}
}

When the Java run-time system detects the attempt to divide by zero, it 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.

In this example, we haven’t supplied any exception
handlers of our own, so the exception is caught by the default handler provided by the
Java run-time system. Any exception that is not caught by your program will ultimately be processed by the default handler.

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.
OUTPUT:

java.lang.ArithmeticExcepti
on: / by zero
at Exc0.main(Exc0.java:4)


Another Program:
------------------------

class Exc1 {
static void subroutine() {
int d = 0;
int a = 10 / d;
}
public static void main(String args[]) {
Exc1.subroutine();
}
}


OUTPUT:

java.lang.ArithmeticException: / by zero
at Exc1.subroutine(Exc1.java:4)
at Exc1.main(Exc1.java:7)



USING TRY AND CATCH
====================

class Exc2 {
public static void main(String args[]) {
int d, a;
try {
d = 0;
a = 42 / d;
System.out.println("Within try");
}

catch (ArithmeticException e) {
System.out.println("Division by zero.");
}
System.out.println("After catch statement.");
}
}


OUTPUT:

Division by zero.
After catch statement.


  • when the program gets an error at a=45/d, then
    an
    exception is thrown, program control transfers out of the try block into the catch block.
  • catch block is executed
  • control does not go back to the try block
  • program terminates after the rest of instructions are executed(after catch)
To make the control go to the try block again add a line:

continue;

after the last statement of catch block.

For example..


class Exc2 {
public static void main(String args[]) {
int d, a;
try {
d = 0;
a = 42 / d;
System.out.println("Within try");
}

catch (ArithmeticException e) {
System.out.println("Division by zero.");
continue;
}
System.out.println("After catch statement.");
}
}

OUTPUT:

Division by zero.
Within try
After catch statement.


Multiple catch Clauses
===================

class MultiCatch {
public static void main(String args[]) {
try {
int a = args.length;
System.out.println("a = " + a);
int b = 42 / a;
int c[] = { 1 };
c[42] = 99;
} catch(ArithmeticException e) {
System.out.println("Divide by 0: " + e);
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array index oob: " + e);
}
System.out.println("After try/catch blocks.");
}
}

OUTPUT(For two cases):

C:\>java MultiCatch
a = 0
Divide by 0: java.lang.ArithmeticException: / by zero
After try/catch blocks.



C:\>java MultiCatch TestArg
a = 1
Array index oob: java.lang.ArrayIndexOutOfBoundsException
After try/catch blocks.

1 comment:

Featured Post 1

notice

Featured Post 2

IETE

Featured Post 6

results

Featured Post 7

WBUT

Featured Post 3

Featured Post 4

Featured Post 5

Featured Post 8