< Browse > Home / Java / Blog article: Java Tutorial : Implementing Exception Handling

| Mobile | RSS

Java Tutorial : Implementing Exception Handling  

14th Oct, 2009 | View Comments | Posted in Java by Alex Jose
Share
 

 

When an unexpected error occurs in a method, Java creates an object of the type Exception. After creating the Exception object, Java sends it to the program by throwing the exception. The Exception object contains information about the type of error and the state of the program when the exception occurred. You need to handle the exception using an exception-handler and process the exception. You can implement exception-handling in a program by using the following keywords:

  • try
  • catch
  • throw
  • throws
  • finally

Using try and catch Statements

The try block encloses the statements that might raise an exception within it and defines the scope of the exception handlers associated with it. If an exception is raised within the try block, the appropriate exception-handler that is associated with the try block processes the exception.

In Java, the catch block is used as an exception-handler. You enclose the code that you want to monitor inside a try block to handle a run-time error. A try block must have at least one catch block that follows it immediately. The catch clause specifies the exception type that you need to catch. The following code shows an arithmetical exception with no exception being handled:

class UnhandledException
{
public static void main(String args[])
{
int num1=0, num2=5;
int num3=num2/num1;
System.out.println ("The num3 = " + num3);
}
}

In the preceding code, the default handler of the Java run-time system throws a raised exception when zero divides a number. It creates an object of the ArithmeticException exception class. The output of the preceding code is:

An Unhandled Exception

The output of the preceding code shows that the exception thrown is the object of the ArithmeticException subclass of the Exception class. The following syntax shows how to declare the try block:

try
{
// Statements that cause an exception.
}

If an exception is raised within a try block, the appropriate exception-handler that is associated with the try block handles the exception.

The catch statement takes the object of the Exception class that refers to the exception caught, as a parameter. When the exception is caught, the statements within the catch block are executed. The scope of the catch block is restricted to the statements in the preceding try block only. The following syntax shows how to declare the try- catch block:

try
{
// Statements that cause an exception.
}
catch(ExceptionName obj)
{
// Error handling code.
}

In the preceding syntax, the catch statement accepts the object of the Exception class that refers to the exception caught, as a parameter. When the exception is caught, the statements within the catch block are executed. You can use the following code to try and catch an arithmetic exception:

class ArithmeticExp
{
public static void main(String args[])
{
int num1=0, num2=5,num3=0;
1B.26 Programming in Java
try
{
num3=num2/num1;
System.out.println("The result=" + num3);
}
catch(ArithmeticException e)

{
System.out.println("Division by zero is performed");
}
}
}

The output of the preceding code is:

Using Try-Catch Statements

 

Using Multiple catch Statements

A single try block can have many catch blocks. This is necessary when the try block has statements that raise different types of exceptions. The following code traps three types of exceptions:

public class TryCatch
{
public static void main(String args[])
{
int array[] = {0,0};
int num1, num2, result = 0;
num1 = 100;
num2 = 0;
try
{
result = num1/num2;
System.out.println(num1 / array[2]);
//more statements
}
catch(ArithmeticException e)
{
System.out.println("Error… Division by zero");
}
Programming in Java 1B.27
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Error… Out of bounds");
}
catch (Exception e)
{
System.out.println("Some other error");
}
System.out.println("The result is: " + result);
//program proceeds
}
}

In the preceding code, the try block has many statements, each of which can result in an exception. Three catch blocks follow the try block, and each catch block handles a different type of exception.

The output of the preceding code is:

Using Multiple Catch Statements

Related Posts with Thumbnails
Follow Discussion

View Comments

Trackbacks

  1. Java Tutorial : Implementing Exception Handling – Part 2 - Rocking Team  

Leave a Reply

blog comments powered by Disqus