Java-Throw Statement

Throw Statement

So far, you have only been catching exceptions that are thrown by the Java run-time system. However, it is possible for your program to throw an exception explicitly, using the throw statement. The general form of throw is shown here:

throw exceptionObject;

Here, exceptionObject must be an object of type Throwable(Throwable is a superclass of all other exception class) or a subclass of Throwable. There are two ways you can obtain a Throwable object: using a parameter in a catch clause, or creating one with the new operator.

The flow of execution stops immediately after the throw statement; any subsequent statements are not executed.



Program Source

class Javaapp {  
    public static void main(String args[]) {
                throw new ArrayIndexOutOfBoundsException();
                System.out.println("This Statement Not Execute");
            }catch(ArrayIndexOutOfBoundsException ae) {
                System.out.println("Throw Generates the Exception Once Again");
                throw ae;
        }catch(ArrayIndexOutOfBoundsException ae)

Leave a Comment