All Java exceptions are instances of classes in the Exception class hierarchy. This can be represented as follows:
Notes:
Checked versus Unchecked Exceptions
One of the criticisms of exception support in some programming languages is that is difficult to know which exceptions a given method or procedure might throw. Given that an unhandled exception is liable to cause a program to crash, this can make exceptions a source of fragility.
The Java language addresses this concern with the checked exception mechanism. First, Java classifies exceptions into two categories:
(In the following, "thrown" refers to any exception thrown explicitly (by a throw statement), or implicitly (in a failed dereference, type cast and so on). Similarly, "propagated" refers to an exception that was thrown in a nested call, and not caught within that call. The sample code below will illustrate this.)
The second part of the checked exception mechanism is that there are restrictions on methods where a checked exception may occur:
In short, a checked exception must be either handled, or declared.
These restrictions do not apply to unchecked exceptions. This includes all cases where an exception is thrown implicitly, since all such cases throw unchecked exceptions.
Checked exception examples
These code snippets are intended to illustrate the checked exception restrictions. In each case, we show a version of the code with a compilation error, and a second version with the error corrected.
// This declares a custom checked exception. public class CustomCheckedException extends Exception { // constructors omitted. } // This declares a custom unchecked exception. public class CustomUncheckedException extends RuntimeException { // constructors omitted. }
The first example shows how explicitly thrown checked exceptions can be declared as "thrown" if they should not be handled in the method.
// INCORRECT public void methodThrowingCheckedException(boolean flag) { int i = 1 / 0; // Compiles OK, throws ArithmeticException if (flag) { throw new CustomCheckedException(); // Compilation error } else { throw new CustomUncheckedException(); // Compiles OK } } // CORRECTED public void methodThrowingCheckedException(boolean flag) throws CustomCheckedException { int i = 1 / 0; // Compiles OK, throws ArithmeticException if (flag) { throw new CustomCheckedException(); // Compilation error } else { throw new CustomUncheckedException(); // Compiles OK } }
The second example shows how a propagated checked exception can be dealt with.
// INCORRECT public void methodWithPropagatedCheckedException() { try { InputStream is = new FileInputStream("someFile.txt"); // Compilation error // FileInputStream throws IOException or a subclass if the file cannot // be opened. IOException is a checked exception. // ... } catch (IOException ex) { System.out.println("Cannot open file: " + ex.getMessage()); } } // CORRECTED (Version A) public void methodWithPropagatedCheckedException() throws IOException { InputStream is = new FileInputStream("someFile.txt"); // ... } // CORRECTED (Version B) public void methodWithPropagatedCheckedException() { try { InputStream is = new FileInputStream("someFile.txt"); // ... } catch (IOException ex) { System.out.println("Cannot open file: " + ex.getMessage()); } }
The final example shows how to deal with a checked exception in a static field initializer.
// INCORRECT public class Test { private static final InputStream is = new FileInputStream("someFile.txt"); // Compilation error } // CORRECTED public class ModifiedTest { private static final InputStream inputStream; static { InputStream tmp = null; try { tmp = new FileInputStream("someFile.txt"); } catch (IOException ex) { System.out.println("Cannot open file: " + ex.getMessage()); } inputStream = tmp; } }
Note that in this last case, we also have to deal with the problems that is cannot be assigned to more than once, and yet also has to be assigned to, even in the case of an exception.
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions