What does throw mean in exception handling


12.3 Passing on exceptions



12.3.1 The catch-or-throw rule

When it comes to handling exceptions in Java, there is the basic rule catch or throw. It states that every exception is either treated or passed on must become. How to handle exceptions was explained using the instruction in the previous sections. If an exception is not to be handled but passed on, this can be done simply by not using a suitable - statement.

Usually there is an error when compiling the program, because the compiler expects that every possible exception that is not handled but passed on must be declared with the help of the clause. To do this, the keyword with a list of all exceptions that should not be handled is appended to the end of the method header:

001 publicvoid SqrtTable () 002 throws ArithmeticException 003 {004 double x = -1.0; 005 006 while (x <= 10.0) {007 System.out.println ("sqrt (" + x + ") =" + Math.sqrt (x)); 008 x + = 1.0; 009} 010}
Listing 12.8. Using the throws clause

12.3.2 Passing on an exception

If an exception occurs, the runtime system first looks for a - statement immediately surrounding the statement that handles this error. If it does not find one, it repeats the search successively in all of the surrounding blocks. If this is also unsuccessful, the error is passed on to the caller of the method. There the search for an error handler begins again, and the error is passed on to the surrounding blocks and finally to the caller. If the main method does not contain any code to handle the error either, the program aborts with an error message.

Since all errors that are not handled within a method are made known to the compiler with the help of the clause, the compiler knows the potential errors that can be caused by each method. With this information, the compiler can ensure that the caller has the catch-or-throw-Rule complies.

The RuntimeException class

There is an exception to this so that the effort involved in explicit error handling when developing Java programs does not become too great catch-or-throw-Rule. The class is directly below the class. It is the parent class of all runtime errors that are handled can, but not have to. In this case, the programmer can decide for himself whether he wants to handle the error in question or not.

12.3.3 Throwing Exceptions

As already mentioned, exception objects under Java are instances of certain classes and can therefore be treated like other objects. It is therefore particularly possible to instantiate an error object or to derive your own error classes from the existing ones.

Creating an object from one fault class is the same as creating an object from any other class. The instruction creates a new error object of the class that can be assigned to a variable of the corresponding type.

With the help of the statement, such an object can be used to generate an exception. The syntax of the statement is:

The handling of these errors follows the usual rules. It corresponds exactly to the case if, instead of the statement, a called method would have triggered the same error: First, an error handler is searched for in the surrounding blocks. If this is unsuccessful, the error is passed on to the caller.

Self-defined exceptions must also adhere to the catch-or-throw- Keep the rule. If the exception is not handled within the same method, it must be declared using the clause in the method head and handled further up in the call chain.

The following example defines a function that determines whether the passed parameter is a prime number. If a negative value is passed, the method throws an exception of the type:

001 publicboolean isPrim (int n) 002 throws ArithmeticException 003 {004 if (n <= 0) {005 thrownew ArithmeticException ("isPrim: Parameter <= 0"); 006} 007 if (n == 1) {008 return false; 009} 010 for (int i = 2; i <= n / 2; ++ i) {011 if (n% i == 0) {012 return false; 013} 014} 015 return true; 016}
Listing 12.9. Throwing an Exception

The instruction has the character of a jump instruction. It interrupts the program at the current point and branches directly to the surrounding clause. If there is not one, the error is passed on to the caller. If the error occurs within a - statement with a - clause, this is executed before the error is passed on.