Exception Handling in Java
An exception in java programming is an abnormal situation that is araised during the program execution. In simple words, an exception is a problem that arises at the time of program execution.
When an exception occurs, it disrupts the program execution flow. When an exception occurs, the program execution gets terminated, and the system generates an error. We use the exception handling mechanism to avoid abnormal termination of program execution.
Java programming language has a very powerful and efficient exception handling mechanism with a large number of built-in classes to handle most of the exceptions automatically.
Java programming language has the following class hierarchy to support the exception handling mechanism.
Reasons for Exception Occurrence
Several reasons lead to the occurrence of an exception. A few of them are as follows.
- When we try to open a file that does not exist may lead to an exception.
- When the user enters invalid input data, it may lead to an exception.
- When a network connection has lost during the program execution may lead to an exception.
- When we try to access the memory beyond the allocated range may lead to an exception.
- The physical device problems may also lead to an exception.
Types of Exception
In java, exceptions have categorized into two types, and they are as follows.
- Checked Exception - An exception that is checked by the compiler at the time of compilation is called a checked exception.
- Unchecked Exception - An exception that can not be caught by the compiler but occurrs at the time of program execution is called an unchecked exception.
How exceptions handled in Java?
In java, the exception handling mechanism uses five keywords namely try, catch, finally, throw, and throws.
We will learn all these concepts in this series of tutorials.