Exception Handling in Java
The exception is an abnormal condition that occurs during the execution of a program and disrupts the normal flow of the program. This exception must be handled properly. If it is not handled, the program will be terminated abruptly.
Exception Handling in Java is one of the powerful mechanisms to handle the runtime errors so that the normal flow of the application can be maintained.
In this tutorial, we will learn more about exceptions in java, types of exceptions, and the difference between checked and unchecked exceptions.
What is Exception Handling?
Exception Handling is a mechanism to handle runtime errors such as IOException, SQLException, ClassNotFoundException, RemoteException, etc.
Major reasons why an exception Occurs
- Invalid user input
- Device failure
- Loss of network connection
- Physical limitations (out of disk memory)
- Code errors
- Opening an unavailable file
Let us discuss the most important part which is the differences between Error and Exception that is as follows:
Error: An Error indicates a serious problem that a reasonable application should not try to catch.
Exception: Exception indicates conditions that a reasonable application might try to catch.
Java Exceptions - Hierarchy
Types of Java Exceptions
- Built-in Exception (Checked, Unchecked, and Error)
- User-Defined Exceptions
Difference between Checked and Unchecked Exceptions
1) Checked Exception
2) Unchecked Exception
3) Error
Java Exception Keywords
- try- The "try" keyword is used to specify a block where we should place an exception code. It means we can't use try block alone. The try block must be followed by either catch or finally.
- catch - The "catch" block is used to handle the exception. It must be preceded by a try block which means we can't use catch block alone. It can be followed by the finally block later.
- finally- The "finally" block is used to execute the necessary code of the program. It is executed whether an exception is handled or not.
- throw- The "throw" keyword is used to throw an exception.
- throws - The "throws" keyword is used to declare exceptions. It specifies that there may occur an exception in the method. It doesn't throw an exception. It is always used with method signature.
Java Exception Handling Example
public class ExceptionHandling1 {
public static void main(String args[]) {
try {
//code that may raise exception
int value = 10 / 0;
} catch (ArithmeticException ex) {
System.out.println(ex);
}
//rest of the code
System.out.println("rest of the program...");
}
}
Output:-
java.lang.ArithmeticException: / by zero
rest of the program...
In the above example, 10/0 raises an ArithmeticException which is handled by a try-catch block.
Some of the scenarios of java exceptions
int value=10/0; //ArithmeticException
String data=null;System.out.println(data.length()); //NullPointerException
String name="abc";int i=Integer.parseInt(name); //NumberFormatException
int arr[]=new int[5];arr[13]=10; //ArrayIndexOutOfBoundsException
0 Comments