by BehindJava
What are checked vs unchecked exceptions in Java
In this tutorial we are going to learn about the differences between checked and unchecked exceptions in Java which is important and regular question for a Java interview.
Firstly We will know and understand why an exception is dividied into checked and unchecked exceptions.
Unchecked Exception
- In case of unchecked Exception your Java application is not connected to any outer resources which means only your Java program is running.
- Unchecked exceptions are unchecked or not checked by the compiler. Since unchecked exceptions are also called as runtime exceptions.
- In unchecked exception handling the exception is optional. If you handle the exception your program will run smoothly without any interuptions else programs execution will stop during runtime execution.
Examples:
- ArthimeticException
- ArrayIndexoutofBoundException
- NumberFormatException
- NullPointerException
Checked Exception
- In case of checked exception Java application is connected to outer resources. Since Java application is connected to the outer resources the compilation check happens to the Java program.
- Checked exceptions are checked by the compiler. Since checked exceptions are also called as compile time exceptions.
- Checked exceptions must be handled properly else compilation doesnt happen and class file will not be generated.
Examples:
- IOException
- FileNotFoundException
- SQLException