Exception Handling In Java
By AmarSivas | | Updated : 2022-05-08 | Viewed : 192 times

Exceptions Handling plays a key role in Java. With Exception Handling, the developer can handle the runtime errors otherwise it may end with unexpected results. The current tutorial illustrates exceptions in Java, types of exceptions, and exception handling in java with nice examples to get a clear idea.
Table of Contents:
What Is Exception In Java
As per the document, an event that occurs in programming execution and causes for disrupting normal execution flow is nothing but the exception.
In other words, in runtime, the normal execution flow is stopped and ends with unexpected results due to some reason is called an exception.
Java Exception Handling Example
We just knew the definition of exception and we focus now on what happens if exception occurs and will also learn to handle the exception here.
package com.docsconsole.tutorials.overloading;
public class Exception {
public void testExecutionFlow(){
//try {
System.out.println(" Statement - 1");//statement 1
System.out.println(" Statement - 2");//statement 2
System.out.println(" Statement - 3");//statement 3
int n = 10/0;//statement 4
System.out.println(" Statement - 4 with variable "+ n); //statement 5
System.out.println(" Statement - 5");//statement 6
System.out.println(" Statement - 6");//statement 7
/*}catch (Exception e){
System.out.println(" Due to exception normal execution flow is disturbed.");
}*/
}
}
If you call the
To avoid abnormal flow, we need exception handling. We need to use the try/catch or throw keyword. Please remove the commented code for try/catch blocks and execute the code. You will not get abnormal termination in line 4., In statement 4, an exception will happen, and control directly reaches to catch block without execution of statements 4,5, and 6. This is how we get the advantages of handling the exceptions.
Exception Hierarchy in Java
Now will try to look at below-given diagram to find the hierarchy of exceptions in java.

Notice the above-given diagram for the hierarchy of exceptions. So it is categorized into two parts those are error and exception. Again the error and exception are classified with different groups. We will learn this entire exception hierarchy in a separate guide.
Types Of Exceptions
Java Exceptions are primarily two types. Those are 1. Checked Exceptions 2. Unchecked Exceptions. Checked Exceptions are getting mainly dealt by the compiler whereas Unchecked exceptions are not getting dealt.
Checked Exceptions Vs Unchecked Exceptions In Java
Checked Exceptions
Checked Exception is an exception that checked by the compiler during compile time.
If code throws some checked exception, then that code either should get wrapped with try/catch block or should get thrown a specific exception using throw keyword. Otherwise, the compiler will provide a syntax error about the checked exception in compile time. So compilation will fail. We need to rewrite the code again to handle the checked exception.
Examples of Checked Exception In Java
-
SQLException
-
IOException
-
ClassNotFoundException
-
InvocationTargetException
-
FileNotfound Exception
try/catch
For example, consider the below example to understand about the checked exception.
public class CheckedExceptionExample {
public void getFileContent() {
String fileName = "wrong file name";
File file = new File(fileName);
FileInputStream stream = new FileInputStream(file);
}
public static void main(String[] args) {
new CheckedExceptionExample().getFileContent();
}
}
In this code, you get the syntax error in compilation as the code has the checked exception to be handled. So we need to wrap the
public class CheckedExceptionExample {
public void getFileContent() {
String fileName = "wrong file name";
File file = new File(fileName);
try {
FileInputStream stream = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new CheckedExceptionExample().getFileContent();
}
}
throw
There is another way to handle the checked exception, which is adding exception in method signature using throw keyword. Here, the caller method or API method should be required to handle the checked exception.
Please notice the below example here main() is the caller method which required to handle the checked exception otherwise, the compiler will again give the compile time error, which means compilation fails.
public class CheckedExceptionExample1 {
public void getFileContent() throws FileNotFoundException {
String fileName = "wrong file name";
File file = new File(fileName);
FileInputStream stream = new FileInputStream(file);
}
public static void main(String[] args) {
new CheckedExceptionExample1().getFileContent();
}
}
Unchecked Exceptions
Unchecked exception is one type of exception that will not check by the compiler during compile time. If any unchecked exception occurred during runtime, then execution flow will get stopped abnormally.
Examples of Unchecked Exception In Java
-
NullPointer Exception
-
IndexOutOfBoundsException
Please see the blow given code for Unchecked Exception. So the compile will not check the exception. If an exception occurred in runtime, then the execution flow will be unreported.
public class UnCheckedExceptionExample {
public void getFileContent() {
String string = null;
Boolean isStringEmpty = string.isEmpty();
System.out.println(isStringEmpty);
}
public static void main(String[] args) {
new UnCheckedExceptionExample().getFileContent();
}
}
We can define user defined exception. To learn more about the custom exception please refer to the tutorialCustom Exception In Java
Git hub referal link for this tutorial is Java-Exception-Handling-Example-App