Use of try and catch code blocks for exception handling in Java

  • 2020-04-01 04:12:54
  • OfStack

Use of Java try and catch
Although the default exception handler provided by the Java runtime system is useful for debugging, you usually want to handle exceptions yourself. This has two advantages. First, it allows you to correct mistakes. Second, it prevents the program from automatically terminating. Most users are annoyed (to say the least) by printing stack traces whenever a program terminates and an error occurs. Fortunately, this is easy to avoid.

To prevent and handle a runtime error, simply put the code you want to monitor into a try block. The try block is followed by a catch clause that describes the type of error you want to catch. Doing this is simple. The following program contains a try block that handles the ArithmeticException exception that results from being divided by zero, and a catch clause.


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 divide-by-zero error
      System.out.println("Division by zero.");
    }
    System.out.println("After catch statement.");
  }
}

The program output is as follows:


Division by zero.
After catch statement.

Note that the call to println() in the try block is never executed. Once an exception is thrown, program control moves from the try block to the catch block. Execution never "returns" from a catch block to a try block. Therefore, 'This will not be printed.'

Will not be displayed. Once the catch statement is executed, program control continues from the following line of the entire try/catch mechanism.

A try and its catch statement form a unit. The catch clause is limited in scope to the statements defined earlier in the try statement. A catch statement cannot catch an exception thrown by another try declaration (unless it is a nested try statement).

Statements protected by a try must be declared within a brace (that is, they must be in a block). You cannot use try alone.

The catch clause is constructed to resolve the exception situation and continue running as if the error did not occur. For example, in the following program, each iteration of the for loop yields two random integers. The two integers are divided by each other and the result is divided by 12345. The final result is in a. If a division operation results in a zero division error, it is caught, a is set to zero, and the program continues.


// Handle an exception and move on.
import java.util.Random;

class HandleError {
  public static void main(String args[]) {
    int a=0, b=0, c=0;
    Random r = new Random();

    for(int i=0; i<32000; i++) {
      try {
        b = r.nextInt();
        c = r.nextInt();
        a = 12345 / (b/c);
      } catch (ArithmeticException e) {
        System.out.println("Division by zero.");
        a = 0; // set a to zero and continue
      }
      System.out.println("a: " + a);
    }
  }
}

Displays a description of an exception

Throwable overloads the toString() method (defined by Object), so it returns a string containing the description of the exception. You can display the description of an exception by passing it a parameter in println(). For example, the catch block of the previous program can be overwritten as


catch (ArithmeticException e) {
  System.out.println("Exception: " + e);
  a = 0; // set a to zero and continue
}

When this version replaces the version in the original program, the program runs under the standard javaJDK interpreter, and each zero-divisor error displays the following message:


  Exception: java.lang.ArithmeticException: / by zero


Although there are no specific values in the context, the ability to display an exception description is valuable in other situations -- especially when you experiment with and debug exceptions.

Use of Java multiple catch statements
In some cases, multiple exceptions may be caused by a single piece of code. To handle this, you can define two or more catch clauses, each of which catches one type of exception. When an exception is thrown, each catch clause is checked in turn, and the first clause that matches the type of exception executes. When a catch statement is executed, the other clauses are bypassed and execution continues after the try/catch block. The following example designs two different types of exceptions:


// Demonstrate multiple catch statements.
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.");
  }
}

Running the program under starting conditions with no command-line arguments results in the exception being divided by zero because a is 0. If you supply a command-line argument, it will survive, setting a to be greater than zero. But it causes an ArrayIndexOutOf BoundsException because the length of the integer array c is 1, and the program tries to assign c[42].

Here is the output of the program running in two different situations:


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.

When you use multiple catch statements, it is important to remember that exception subclasses must be used before any of their superclasses. This is because the catch statement for the parent class catches exceptions for that type and all its subtypes. This way, if the subclass is behind the parent, the subclass will never arrive. Also, unreachable code in Java is an error. For example, consider the following program:



class SuperSubCatch {
  public static void main(String args[]) {
    try {
      int a = 0;
      int b = 42 / a;
    } catch(Exception e) {
      System.out.println("Generic Exception catch.");
    }
    
    catch(ArithmeticException e) { // ERROR - unreachable
      System.out.println("This is never reached.");
    }
  }
}

If you try to compile the program, you will receive an error message saying that the second catch statement will not arrive because the exception has been caught. Because arithmeexception is a subclass of Exception, the first catch statement handles all exception-oriented errors, including arithmeexception. This means that the second catch statement will never execute. To modify the program, reverse the order of the two catch statements.


Related articles: