What is Exception handling?
Exception handling is a programming language construct or computer hardware mechanism designed to handle the occurrence of some condition that changes the normal flow of execution. The condition is called an exception. if the Exception condition is met the execution will swith to the predefined Handler. The Handler of that particular scenario will be executed.
Exceptions in java are any abnormal, unexpected events or extraordinary conditions that may occur at program runtime that disrupts the normal flow of instructions. On such conditions java throws an exception object. Java Exceptions are basically Java objects.
What is as throwing an exception?
When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.
What is catching an exception?
The exception handler chosen is said to catch the exception. If the runtime system exhaustively searches for an appropriate exception handler which can handle this scenario and make the program execute furthur. If the runtime system is not able to find a handler it will terminate.
How Exception Handling is used java?
Code that might throw certain exceptions must be enclosed by either of the following:
- 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 must provide a throws clause that lists the exceptions. In this the method which is calling this method need to have the handler defined for the Exception.
Syntax:
try {
code that might throw exception
} catch(Handler){ }
and finally blocks . . .
What is finally block?
The finally block always executes whenever the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs i.e even the exception is not handled. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break statements. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.
The Three Kinds of Exceptions:
- Checked Exception: These are exceptional conditions that a well-written application should anticipate and recover from.
- Error: These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from.
- Runtime Exception: These are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from.
Checked Exception
These are exceptional conditions that a well-written application should anticipate and recover from.
Suppose an application prompts a user for an input file name, then opens the file by passing the name to the java.io.FileReader. Normally, the user expected tot provide the name of an existing, readable file, so the FileReader object succeeds, and the execution of the application proceeds normally. But sometimes the user may supply the name of a nonexistent/wrong file, and the then the FileReader throws java.io.FileNotFoundException. A well-written program will catch this exception and notify the user of the condition, possibly prompting again for a corrected file name.
Error
These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from.
Suppose that an application successfully opens a file provided by the user, but is unable to read/write into/from the file because of a hardware or system malfunction. The unsuccessful read/write will throw java.io.IOError. An application might choose to catch this exception, in order to notify the problem to the user of the application — but it also might make sense to print a stack trace and exit.
Runtime Exception
These are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from. These usually indicate programming bugs, such as logic errors or improper use of an API.
Suppose consider the application described previously that passes a file name to FileReader. If because of a logic error which will pass a null to FileReader, it will throw NullPointerException. The application can catch this exception, but it may probably makes more sense to eliminate the bug that caused the exception to occur instead continue the execution.
Errors and Runtime Exceptions are collectively known as unchecked exceptions.