Java throw: exception throw
The program can use the throw statement to throw clear exceptions. The usual form of a Throw statement is as follows:
throw ThrowableInstance;
Here, ThrowableInstance must be an object of the Throwable class type or Throwable subclass type. Simple types, such as int or char, and non-Throwable classes, such as String or Object, cannot be used as exceptions. There are two ways to get Throwable objects: use parameters in the catch clause or create them with the new operator.
Program execution stops immediately after the throw statement; any subsequent statements are not executed. The tightest-enclosed try block is used to check whether it contains a catch statement that matches the exception type. If a matching block is found, control turns to the statement; if no, the next surrounded try block is checked, and so on. If no matching catch block is found, the default exception handler interrupts the execution of the program and prints the stack trace.
The following is an example program that creates and throws exceptions. The handler that matches the exception will then throw it to the outer handler.
// Demonstrate throw.class ThrowDemo { static void demoproc() { try { throw new NullPointerException("demo"); } catch(NullPointerException e) { Syst em.out.println("Caught inside demoproc."); throw e; / / rethrow the exception } } public static void main(String args[]) { try { demoproc(); } catch(NullPointerException e) { System.out.println("Recau ght: " + e); } }} The program has two opportunities to handle the same error. First, main() sets up an exception relationship and then calls demoproc( ). The demoproc( ) method then sets up another exception handling relationship and immediately throws a new NullPointerException instance, which is captured on the next line. The exception is then thrown again. Here is the output result:
Caught inside demoproc.Recaught: java.lang.NullPointerException: demo
The program also explains how to create standard exception objects in Java, paying special attention to the following line:
throw new NullPointerException("demo");
Here, new is used to construct a NullPointerException instance. All Java built-in runtime exceptions have two constructors: one has no parameters and one has a string parameter. When the second form is used, the parameter specifies the string describing the exception. If the object is used as a parameter to print() or println(), the string is displayed. This can also be achieved by calling getMessage( ), which is defined by Throwable.
Java throws clause <br />If a method can cause an exception but does not handle it, it must specify this behavior so that the caller of the method can protect themselves without the exception. To do this you can include a throws clause in the method declaration. A throws clause lists all exception types that a method may throw. This is necessary for all exceptions of types except Error or RuntimeException and their subclasses. All other types of exceptions that a method can throw must be declared in the throws clause. Failure to do so will result in a compilation error.
Here is a common form of a method declaration containing a throws clause:
type method-name(parameter-list) throws exception-list{ // body of method}Here, exception-list is a comma-segmented exception list that the method can throw.
Here is an incorrect example. This example attempts to throw an exception that it cannot catch. Because the program does not specify a throws clause to declare the fact, the program will not compile.
// This program contains an error and will not compile.class ThrowsDemo { static void throwOne() { System.out.println("Inside throwOne."); throw new Il legalAccessException("demo"); } public static void main(String args[]) { throwOne(); }} To compile the program, two places need to be changed. First, you need to declare throwOne( ) to raise an IllegalAccess Exception. Second, main( ) must define a try/catch statement to catch the exception. The correct examples are as follows:
// This is now correct.class ThrowsDemo { static void throwOne() throws IllegalAccessException { System.out.println("Inside throwOne."); throw new IllegalAccessException("demo"); } public static void main(String args[] ) { try { throwOne(); } catch (IllegalAccessException e) { System.out.println("Caught " + e); } }} Here are the output results of the example:
inside throwOnecaught java.lang.IllegalAccessException: demo