Exception Handling in Java And to Use it in Programming

Google+ Pinterest LinkedIn Tumblr +

Computer Languages that do not support error at run time ,then that error must be manually handled by the programmer.Java  Exception handling avoids these problem and provide run time error handling into the Object Oreinted World.

Java Exception represent an object that describe an error /condition has occur in code.That object must be thrown  in the method where the error has occured.Java Exception handling is manages by five keywords :

1) try,

2)catch,

3)throw,

4)throws and

5)finally.

This is the general form of an exception-handling block:
try {
// block of code to monitor for errors
}
catch (ExceptionType1 e) {
// exception handler for ExceptionType1
}
catch (ExceptionType2 e) {
// exception handler for ExceptionType2
}

finally {
// block of code to be executed before try block ends
}

Now lets take an example in which an error occurs.As we know divisible by zero gives you an error as in this code

class Excp {
public static void main(String args[]) {
int k = 0;
int i = 42 / k;
}
}

If you compile this program it will give you an error .ArithmeticException Divisible by zero.

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 Excp to stop, because once an exception has been thrown, it must be cought.Now we make use of try catch block in order to avoid this error at cmpile time.As soon you come to the end of try block cat statement must be followed.Here is our example

Try-Catch :

class Excp {
public static void main(String args[]) {

try

{

int k = 0;
int i = 42 / k;

}

catch(ArithmeticException e)

{

System.out.println(“Division by zero”);

}

}
}

When you compile this program Java-run time find an exception thrown the program control transfer out of try block to catch block .After executing the catch block the program control continues to the next line of the code.So here we get an error free program.

Throw :It is possible to throw an exception explicitly,using the throw statement. The general form of throw is shown here:
throw ThrowableInstance;

ThrowableInstance must be an object of type Throwable or a subclass of Throwable. Simple types, such as int or char, as well as non-Throwable classes, such as String and Object, cannot be used as exceptions. There are two ways you can obtain a Throwable object: using a parameter into a catch clause, or creating one with the new operator.

sample program that creates and throws an exception. The handler that catches the exception rethrows it to the outer handler.

class ThrowDemo {
static void demo() {
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 {
demo();
} catch(NullPointerException e) {
System.out.println(“Recaught: ” + e);
}
}
}

Throws : A throws clause lists the types of exceptions that a method might throw. It necessary for all exceptions, except those of type Error or RuntimeException, or any of their subclasses.

This is the general form of a method declaration that includes a throws clause:
type method-name(parameter-list) throws exception-list
{
// body of method
}

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.

Share.

About Author

Leave A Reply