- Exception
- is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions
Any Java programming language code must honor the Catch or Specify Requirement. That is, a set of instructions that can throw exceptions must by follow either of the following two rules:
- A try statement that catches the exception. The try must provide a handler for the exception
- A method that specifies that it can throw the exception. The method must provide a throws clause that lists the exception.
Kinds of Exceptions
- Check Exceptions: These are exceptional conditions that a well-written application should anticipate and recover from. For example, suppose the application accepts an array of integers from the commandline, but if the number of integers exceed the array size limit defined in the program then the application should prompt the user to re-enter correct number of integers. Checked Exceptions are subject to Catch or Specify requirement while other exceptions do not comply.
RuntimeException, and their subclases. - Error: These are conditions that occur external to the application. Hardware failures is the best example of such conditions.
- Runtime Exception: These are internal application faults that the application cannot anticipate. These usually indicate programming bugs. Rather than catching these exceptions and handling them it is better to eliminate the bug that caused the exception to occur.
try, catch and finally blocks
- try block
- it contains the code that might throw an exception
- catch block
- it contains the code required to handle an exception thrown from a try block.
- finally block
- it contains code, that always executes when the try block exits.
Specifying Exceptions with the throws Clause
If we do not want to specify a catch block(s) for the exceptions occurring in a method, but would like some method higher in the call stack to handle it, then we can just specify the type of exceptions the method throws in the method declaration itself.Example:Since, ArrayIndexOutOfBoundsException is an unchecked exception, including it in the throws clause is not mandatory.
public void writeList() throws IOException, ArrayIndexOutOfBoundsException {
throw Statement
Inorder, to catch an exception object, it should be thrown at the first place. The throw keyword is used by methods, in this context, to throw exception objects.Example:The above example, is a code taken partly from the pop() method of common stack object. Here, if the size of the stack is empty it throws an object of EmptyStackException() exception class.
if(size == 0){
throw new EmptyStackException();
}
Throwable Class
All exception-objects are objects of Throwable class or its subclasses. Error and Exception are the first descendants of throwable class.Chained Exception
An exception can throw another exception, hence forming a chain of exceptions. This is possible, in general by throwing an exception inside a catch block. The following methods and constructors in Throwable support chained exceptions:- Throwable getCause()
- Throwable initCause(Throwable)
- Throwable(String, Throwable)
- Throwable(Throwable)
Stack Trace Information
- Stack Trace
- it provides information on the execution history of the current thread and lists the names of the classes and methods that were called at the point when the exception occurred.
Creating Exception Classes
The Java Platform, provides a wide range of exception classes for use. If for any purpose, you require your own exception classrelated to the program then you can manually create. All exception-classes can inherit any of the subclasses of Exception class. But it would be more appropriate to inherit Exception only.
tutorials
No comments:
Post a Comment