Home>

java uncaught exception

Before you learn to handle exceptions in your program,It's good to see what happens if you don't deal with them.The following applet includes an expression that intentionally causes a division by zero error.

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

When the java runtime system detects a division by zero,It constructs a new exception object and then throws the exception.This caused the execution of exc0 to stop,Because once an exception is thrown,It must be caught by an exception handler and processed immediately.In this example, we did not provide any of our own exception handlers,So the exception is caught by the default handler of the java runtime system.Any exceptions not caught by your program will eventually be handled by the default handler.The default handler displays a string describing the exception,Print the stack trace where the exception occurred and terminate the program.

Here is the output produced by the program executed by the standard javajdk runtime interpreter:

 java.lang.arithmeticexception:/by zero
  at exc0.main (exc0.java:4)

Note how the class name exc0, method name main, file name exc0.java, and line number 4 are included in a simple stack usage trace.Also, note that the type of exception thrown is a subclass of exception called arithmeticexception. This subclass more clearly describes what type of error method.As discussed later in this chapter,Java provides multiple built-in exception types that match different kinds of runtime errors that may be generated.

The stack trace will show the sequence of method calls that caused the error.For example, here is another version of the previous program,It introduced the same error,But the error is generated in another method besides the main () method:

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

The stack trace results of the default exception handler show how the entire call stack is displayed:

 java.lang.arithmeticexception:/by zero
  at exc1.subroutine (exc1.java:4)
  at exc1.main (exc1.java:7)

As you can see,At the bottom of the stack is line 7 of main, which calls the subroutine () method. This method caused an exception on line 4.The call stack is important for debugging,Because it pinpoints the exact steps that led to the error.

Java try statement nesting

Try statements can be nested.That is,A try statement can be inside another try block. Each time a try statement is entered, the context of the exception is pushed onto the stack.If an internal try statement contains no special exception catch handler,The stack will pop up,The catch handler of the next try statement will check for a match.This process will continue until a catch statement matches successfully,Or until all nested try statements are exhausted.If no catch statement matches,The java runtime system will handle this exception.The following is an example of using nested try statements:

//an example of nested try statements.
class nesttry {
  public static void main (string args []) {
    try {
      int a=args.length;
      /* if no command-line args are present, the following statement will generate a divide-by-zero exception. * /
      int b=42/a;
      system.out.println ("a =" + a);
      try {//nested try block
        /* if one command-line arg is used, then a divide-by-zero exception will be generated by the following code. * /
        if (a == 1) a=a/(a-a);//division by zero
        /* if two command-line args are used, then generate an out-of-bounds exception. * /
        if (a == 2) {
          int c []={1};
          c [42]=99;//generate an out-of-bounds exception
        }
      } catch (arrayindexoutofboundsexception e) {
        system.out.println ("array index out-of-bounds:" + e);
      }
    } catch (arithmeticexception e) {
      system.out.println ("divide by 0:" + e);
    }
  }
}

As you can see,This program has another try block nested within a try block. The program works as follows:When you execute the program without command line arguments,The outer try block will generate an exception divided by zero.The program is executed with a command line parameter,A division by zero error is generated by a nested try block.Because the inner block does not match this exception,It will pass the exception to the outer try block, where the exception is handled.If you execute the program with two command line arguments,An array boundary exception was generated by an internal try block.The following results illustrate each situation:

c:\>java nesttry
divide by 0:java.lang.arithmeticexception:/by zero
c:\>java nesttry one
a=1
divide by 0:java.lang.arithmeticexception:/by zero
c:\>java nesttry one two
a=2
array index out-of-bounds:java.lang.arrayindexoutofboundsexception

When there is a method call,The nesting of try statements can happen very subtly.For example, you can put a call to a method in a try block. Inside the method,There is another try statement. In this situation,The try inside the method is still nested inside the try block that calls the method outside. Here is a modification of the previous example,The nested try block is moved inside the method nesttry ():

/* try statements can be implicitly nested via calls to methods. * /
class methnesttry {
  static void nesttry (int a) {
    try {//nested try block
      /* if one command-line arg is used, then a divide-by-zero exception will be generated by the following code. * /
      if (a == 1) a=a/(a-a);//division by zero
      /* if two command-line args are used, then generate an out-of-bounds exception. * /
      if (a == 2) {
        int c []={1};
        c [42]=99;//generate an out-of-bounds exception
      }
    } catch (arrayindexoutofboundsexception e) {
      system.out.println ("array index out-of-bounds:" + e);
    }
  }
  public static void main (string args []) {
    try {
      int a=args.length;
      /* if no command-line args are present, the following statement will generate a divide-by-zero exception. * /
      int b=42/a;
      system.out.println ("a =" + a);
      nesttry (a);
    } catch (arithmeticexception e) {
      system.out.println ("divide by 0:" + e);
    }
  }
}

The output of this program is the same as the previous example.

  • Previous C # operation IIS method collection
  • Next Winform method for limiting and removing the range of mouse movement