Built-in exception subclass
In the standard package java.lang, Java defines several exception classes. Some of these were used in the previous examples. These exceptions are generally subclasses of the standard class RuntimeException. Because java.lang is actually introduced by all Java programs, most exceptions derived from RuntimeException are automatically available. Also, they do not need to be included in the throws list of any method. In Java, this is called an unchecked exception. Because the compiler does not check it to see if a method handles or throws these exceptions. Unchecked exceptions defined in java.lang are listed in Table 1. Table 2 lists exceptions that must be included in the throws list of methods defined by java.lang, if these methods can generate one of the exceptions but cannot handle it by themselves. These are called checked exceptions. Java defines several other exception types related to different library classes.
Table 1: Unchecked exception subclass defined in java.lang in Java
Table 2: Check exception defined in java.lang
Create your own exception subclass using Java
Although Java's built-in exception handling most common errors, you may want to build your own exception type to handle the special cases you are applying. This is very simple: just define a subclass of Exception (Exception is of course a subclass of Throwable). Your subclasses don't need to actually do anything - their presence in the type system allows you to use them as exceptions.
The Exception class itself does not define any method. Of course, it inherits some of the methods provided by Throwable. Therefore, all exceptions, including those you create, can get the Throwable defined method. These methods are shown in Table 3. You can also override one or more of these methods in the exception class you create.
Table 3 Methods defined by Throwable
The following example declares a new subclass of Exception, which is then used as a signal for an error in the method. It overloads the toString( ) method, so that the exception description can be displayed with println( ).
// This program creates a custom exception type.class MyException extends Exception { private int detail; MyException(int a) { detail = a; } public Str ing toString() { return "MyException[" + detail + "]"; }} class ExceptionDemo { static void compute(int a) throws MyException { System.out.println("Called compute(" + a + ")"); if(a > 10) throw new MyExcepti on(a); System.out.println ("Normal exit"); } public static void main(String args[]) { try { compute(1); compute(20); } catch (MyException e) { System.out.println("Caught " + e) ; } }}
This example defines a subclass of Exception MyException. This subclass is very simple: it contains only a constructor and an overloaded toString( ) method that displays outliers. The ExceptionDemo class defines a compute( ) method. This method throws a MyException object. This exception is raised when the integer parameter of compute( ) is larger than 10.
The main( ) method sets up an exception handler for MyException, and then calls compute( ) with a legal value and an illegal value to display different paths of execution through the code. Here are the results:
Called compute(1)Normal exitCalled compute(20)Caught MyException[20]