Java is an object-oriented programming language. It provides support for various mechanisms such as exception handling. This feature of Java enables developers to manage the runtime errors caused by the exceptions.
What is Exception handling in Java?
Exception Handling in Java is one of the effective means to handle the runtime errors so that the regular flow of the application can be preserved. Java Exception Handling is a mechanism to handle runtime errors such as Class Not Found Exception, IO Exception, SQL Exception, Remote Exception, etc.
What is an Exception?
An exception is an unwanted or unexpected event, which occurs during the execution of a Java program i.e at run time, that disrupts the normal flow of the program’s instructions. Exceptions can be caught and handled by the program. When an exception occurs within a method, it creates an object. This object is called the exception object. It contains information about the exception such as the name and description of the exception and the state of the program when the exception occurred.
Why do Exception Occur?
Most common run-time errors are:
- Division by Zero.
- Accessing an element that is out of the range of array.
- Passing a parameter that is not in a valid range.
- Attempting to use a negative size for an array.
- The file on tries to open may not exist.
- The class file one wants to load may be missing or in the wrong format
When such an error occurs in a program, it creates an exception object & hands it to the runtime system.
The exception object contains the necessary information. about the exception including its types and state of program when the error occurred.
The runtime system is then responsible for finding the code to handle this error. In java terminology creating an exception objects & handling it to the runtime system is called throwing an exception
After throwing an exception, the runtime system FInds a method that contains an exception handler.
The exception handler is chosen to ‘Catch the exception’.
- If the exception object is not caught & handled property, the interpreter will display an error msg & will terminate the program.
If we want the program to Continue with the execution of the remaining Code, Then we should try to catch the exception object thrown by the error condition & then display an appropriate msg. For taking corrective action. This task is known as exception handling.
There are mainly two types of exceptions in Java as follows:
- Build In Exception
- Checked exception
- Unchecked exception
2. User defined Exception
1. Built-in Exceptions: Built-in exceptions are the exceptions that are available in Java libraries. These exceptions are suitable to explain certain error situations.
- Checked exception
Checked exceptions are also known as compile-time exceptions as these exceptions are checked by the compiler during the compilation process to confirm whether the exception is handled by the programmer or not. If not, then the system displays a compilation error. For example, SQLException, IOException, InvocationTargetException, and ClassNotFoundException.
- Unchecked exception
The unchecked exceptions are those exceptions that occur during the execution of the program. Hence they are also referred to as Runtime exceptions. These exceptions are generally ignored during the compilation process. They are not checked while compiling the program. For example, programming bugs like logical errors, and using incorrect APIs.
2. User-Defined Exceptions: Sometimes, the built-in exceptions in Java are not able to describe a certain situation. In such cases, users can also create exceptions which are called ‘user-defined Exceptions’.
The advantages of Exception Handling in Java are as follows:
- Provision to Complete Program Execution
- Easy Identification of Program Code and Error-Handling Code
- Propagation of Errors
- Meaningful Error Reporting
- Identifying Error Types
You Might Like:
ADVANTAGES AND DISADVANTAGES OF JAVA