What is an exception?
The meaning of exception is an abnormal condition, In Java, an exception or exceptional events are the problems that occur during program execution. An unexpected exception can interrupt the normal flow of the program and the program/application may terminate abnormally. To overcome this problem most of the OOP language provides a powerful mechanism called exception handling.
Types of Java exceptions
Java provides three different types of exceptions –
- Checked exception
- Unchecked exception
- Error
Checked exception
A checked exception or well known as a compile-time exception is a type of exception were possible exceptions are checked and notified by the compiler at compile-time. A programmer must need to handle these exceptions.
Example
In the following example program, we are trying to read a file “sample.txt” using Java FileReader class.
import java.io.FileReader; public class CheckedExceptionExample { public static void main(String[] args) { String fileName = "sample.txt"; FileReader reader = new FileReader(fileName); } }
If you try to compile the above-shown program it will give you the following exception:
>> javac CheckedExceptionExample.java CheckedExceptionExample.java:8: error: unreported exception FileNotFoundException; must be caught or declared to be thrown FileReader reader = new FileReader(fileName); ^ 1 error
The compiler is throwing this exception because you must previously handle FileNotException, So that in case of file not found program should not terminate abnormally.
Unchecked exception
An unchecked exception or well known as a runtime-time exception is a type of exception which occur during the execution of the program. These type of exception includes programming bugs, such as logic errors or improper use of an API, etc. Opposite to the checked exceptions unchecked exceptions are ignored at the time of compilation.
Example
In the following example program, we are dividing 10 by 0.
public class UncheckedExceptionExample { public static void main(String[] args) { int numA = 10; int numB = 0; System.out.println(numA / numB); } }
If you try to compile it will not give any error but when you run, it will give you the following exception:
>> javac UncheckedExceptionExample.java >> java UncheckedExceptionExample Exception in thread "main" java.lang.ArithmeticException: / by zero at Main.main(UncheckedExceptionExample.java:8)
It happens because we all know that we cannot divide a number by 0.
Error
Now you well understood what checked and unchecked exceptions are then what is an error? While execution sometimes our program lacks resources, memory and other related things, these things are internal need of a program and whenever it happens an error occurs. Errors are irrecoverable eg. OutOfMemoryError, VirtualMachineError, AssertionError etc.
Example
In the following example program, we are repeating a line “Studyfied <LEARN EASILY FREELY/>” 2,147,483,647 (Integer.MAX_VALUE) times.
public class ErrorExample { public static void main(String[] args) { String line = "Studyfied <LEARN EASILY FREELY/>"; for (int i = 0; i < Integer.MAX_VALUE; i++) { line += line; } } }
If you try to compile it will not give any error but when you run, it will give you the following exception:
>> javac ErrorExample.java >> java ErrorExample Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Unknown Source) at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source) at java.lang.AbstractStringBuilder.append(Unknown Source) at java.lang.StringBuilder.append(Unknown Source) at ErrorExample.main(ErrorExample.java:9)
It happens because our program is failed to handle that much big string, or it is going beyond its memory limit.