JDB - Exception



This chapter explains how to handle the exception class using JDB. Generally, whenever a program raises an exception without a catch statement, then the VM prints the exception line, the cause of the exception, and exits. If the exception has been raised with a catch statement, then the exception is handled by the catch statement. Here, the VM prints the output with the cause of exception.

When the class that raises the exception is running under JDB, it also throws the uncaught exception. That exception can be handled using the catch command.

Example

Let us take an example of the class JdbException:

public class JdbException
{
   public static void main(String ar[]) throws Exception
   {
      int a=8, b=0;
      System.out.println("Welcome");
      System.out.println("Ex: "+(a/b));
   }
}

Save the above file with the name JdbException.java. Compile this file using the following command:

\>javac JdbException.java

Follow the steps given below to handle the exception.

Step 1: Run the Class

The following command executes the class named JdbException as follows:

\>jdb JdbException
>run

This JdbException class contains an exception, hence you get to see the following output:

Exception1

Step 2: Catch the Exception

The following command catches the exception:

mian[1] catch java.lang.ArithmeticException

It will give you the following output:

Set all java.lang.ArithmeticException

Step 3: Continue Execution

The following command continues the execution. Now the catch handles the arithmetic exception as follows:

Exception2
Advertisements