An exception is a runtime error. There are two types of exceptions known as checked and unchecked exceptions. When a checked exception occurs, the Java application is connected to an outside resource such as a file, device or database. These exceptions are checked by the compiler. Some examples of checked exceptions are IO exception and FileNotFound exception. When an unchecked exception occurs, the application is not connected to any outside resource. These exceptions are not checked by the compiler. Some examples of unchecked exceptions are Arithmetic Exception and ArrayOutOfBound Exception. This article discusses the difference between checked and unchecked exception in Java. The key difference between checked and unchecked exception in Java is that a checked exception is checked by the compiler while an unchecked exception is not checked by the compiler.
CONTENTS
1. Overview and Key Difference
2. What is Checked Exception in Java
3. What is Unchecked Exception in Java
4. Similarities Between Checked and Unchecked Exception in Java
5. Side by Side Comparison – Checked vs Unchecked Exception in Java in Tabular Form
6. Summary
What is Checked Exception in Java?
When a checked exception occurs, the Java application is connected to an outside resource. This resource can be a device such as printer. It can be a file or a database. Therefore, those exceptions are checked by the compiler. IO exception is a checked exception. It occurs due to an error in the device. When the application is accessing a file that does not exist, then it will cause a FileNotFound exception. An application can be connected to a database such as MySQL, Oracle etc. to store data. If an error occurred related to a database, it would be an SQL Exception. Those are some examples of checked exceptions. In all these, the application is connected to an outer resource. In checked exceptions, it is mandatory to handle the exception. If it is not handled, the correct flow of the program will be terminated, and the class file will not be generated. The error can be handled using the try, catch block.
According to the above, the FileReader read data from the file. The text1.txt file does not exist in the specified location. The code that can give the exception is placed inside the try block. The message to print is inside the catch block. As there is no file called text1.txt, this causes a FileNotFoundException. By using exception handling, the message is printed to the screen.
What is Unchecked Exceptions in Java?
Unchecked exceptions are not checked by the compiler. Unlike in checked exceptions, with unchecked exceptions, the Java application is not connected to an outer resource such as file, database or a device. Some common unchecked exceptions are Arithmetic, ArrayOutOfBound and NullPointer Exceptions.
int a = 10, b = 0;
int div = a/b;
System.out.println(div);
This will cause an arithmetic exception because of diving ‘a’ by zero. Refer the below code.
According to the above program, variable a is an integer value. Variable b is 0. The division of these two numbers is a divide by zero. Therefore, it will cause an arithmetic exception. It can be handled using try-catch block. The statements that can cause the exception is placed inside the try block. The message to be displayed is in the catch block.
Refer the below piece of the code.
int array1[] = {1,2,3,4,5};
System.out.println(array1[5]);
This will cause an exception. The array1 is an array with 5 elements. The starting index of the array is zero. Printing the 5th index value causes an exception because it is out of bound. The maximum index of the array1 is 4.
According to the above program, the array1 has 5 elements. Printing the element with index 6 will cause an exception because it is out of bound. The maximum index that can be stored in array1 is 5. The error message prints by executing the catch block.
What is the Similarity Between Checked and Unchecked Exception in Java
- Both Checked and Unchecked Exception in Java are exception types in Java.
What is the Difference Between Checked and Unchecked Exception in Java?
Checked vs Unchecked Exception in Java |
|
A checked exception is a runtime error that is checked by the compiler. | An unchecked exception is a runtime error that is not checked by the compiler. |
Occurrence | |
When a checked exception occurs, the Java application is connected to an outer resource such as file, device or a database. | When an unchecked exception occurs, the Java application is not connected to an outer resource. |
Examples | |
IOException, FileNotFoundException, SQLException are some examples of checked exceptions. | The arithmetic exception, ArrayOutOfBoundException, NullPointerException are some examples of unchecked exceptions. |
Summary – Checked vs Unchecked Exception in Java
An exception is an event that interrupts the execution of the program flow. There are two types of exceptions. They are called checked exceptions and unchecked exceptions. This article discussed the difference between a checked exception and unchecked exceptions. The difference between checked and unchecked exception in Java is that a checked exception is checked by the compiler while an unchecked exception is not checked by the compiler. As exceptions affect the correct flow of program execution, it is a good programming practice to handle them.
Reference:
1.Point, Tutorials. “Java Exceptions.”, Tutorials Point, 8 Jan. 2018. Available here
2.nareshtechnologies. Checked vs Unchecked Exception || Core Java FAQs Videos, Naresh i Technologies, 31 Dec. 2016. Available here