All exception types are subclasses of the built-in class Throwable. Thus, Throwable is at the top of the exception class hierarchy. Immediately below Throwable are two subclasses that partition exceptions into two distinct branches.
- Exception.
- Error
Exception This class is used for exceptional conditions that user programs should catch. This is also the class that you will subclass to create your own custom exception types. There is an important subclass of Exception, called RuntimeException. Exceptions of this type are automatically defined for the programs that written and include things such as division by zero and invalid array indexing.
Error, which defines exceptions that are not expected to be caught under normal circumstances by your program. Exceptions of type Error are used by the Java run-time system to indicate errors having to do with the run-time environment, itself. Stack overflow is an example of such an error. This chapter will not be dealing with exceptions of type Error, because these are typically created in response to catastrophic failures that cannot usually be handled by the program.
Exception Types
In Java there are basically two types of exceptions:
1. Checked exceptions
2. unchecked exceptions
Checked exceptions must be explicitly caught or propagated as described in basic try-catch-finally exception handling . Unchecked exceptions do not have this requirement. They don't have to be caught or declared thrown.
Checked exceptions in Java extend the java.lang.Exception class. Unchecked exceptions extend the java.lang.RuntimeException.
Checked and unchecked exceptions are functionally equivalent. There is nothing you can do with checked exceptions that cannot also be done with unchecked exceptions, and vice-versa.
Java’s Built-in Exceptions Or Unchecked Exception
Inside the standard package java.lang, Java defines several exception classes. The most general of these exceptions are subclasses of the standard type runtimeException.
Since java.lang is implicitly imported into all Java programs, most exceptions derived from RuntimeException are automatically available. Furthermore, they need not be included in any method’s throws list.
In the language of Java, these are called unchecked exceptions because the compiler does not check to see if a method handles or throws these exceptions.
Published by : Ruchita Pandya
No comments:
Post a Comment