
Exception Handling
Exception Handling is a mechanism to handle runtime errors such as ClassNotFoundException, IOException, SQLException, RemoteException,ArthmeticException,NullPointerException etc.
Exception is an unwanted or unexpected event, which occurs during the execution of a 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.
Differences between Error and Exception that is as follows:
An Error indicates a serious problem that a reasonable application should not try to catch.
Exception indicates conditions that a reasonable application might try to catch
Java defines several types of exceptions that relate to its various class libraries. Java also allows users to define their own exceptions.
Exceptions can be categorized in two ways:
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’.
Built-in exceptions that are available in Java libraries. These exceptions are suitable to explain certain error situations.
Unchecked Exceptions:
The unchecked exceptions are just opposite to the checked exceptions. The compiler will not check these exceptions at compile time. In simple words, if a program throws an unchecked exception, and even if we didn’t handle or declare it, the program would not give a compilation error.
The core advantage of exception handling is to maintain the normal flow of the application. An exception normally disrupts the normal flow of the application; that is why we need to handle exceptions. Let's consider a scenario:
Example statement 1; statement 2; statement 3; statement 4; statement 5;//exception occurs statement 6; statement 7; statement 8; statement 9; statement 10; |
Suppose there are 10 statements in a Java program and an exception occurs at statement 5; the rest of the code will not be executed, i.e., statements 6 to 10 will not be executed. However, when we perform exception handling, the rest of the statements will be executed.
public class JavaExceptionExample{
public static void main(String args[]){ try{ //code that may raise exception int data=100/0; }catch(ArithmeticException e){ System.out.println(e); } } //rest code of the program int a=10+5; System.out.println("rest of the code..."); System.out.println(a); } Out put// java.lang.ArithmeticException: / by zero rest of the code... a=15 |
In the above example, 100/0 raises an ArithmeticException which is handled by a try-catch block.
throw
Java throw keyword is used to throw an exception explicitly in the code, inside the function, or the block of code.
using throw only
throws
Java throw keyword is used in the method signature to declare an exception that might be thrown by the function while the execution of the code