- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Checked vs Unchecked exceptions in Javan
Checked exceptions
A checked exception is an exception that occurs at the compile time, these are also called as compile time exceptions. These exceptions cannot simply be ignored at the time of compilation, the programmer should take care of (handle) these exceptions.
For example, if you use FileReader class in your program to read data from a file, if the file specified in its constructor doesn't exist, then a FileNotFoundException occurs, and the compiler prompts the programmer to handle the exception.
Example
import java.io.File; import java.io.FileReader; public class FilenotFound_Demo { public static void main(String args[]) { File file = new File("E://file.txt"); FileReader fr = new FileReader(file); } }
If you try to compile the above program, you will get the following exceptions.
Output
C:\>javac FilenotFound_Demo.java FilenotFound_Demo.java:8: error: unreported exception FileNotFoundException; must be caught or declared to be thrown FileReader fr = new FileReader(file); ^ 1 error
Note − Since the methods read() and close() of FileReader class throws IOException, you can observe that the compiler notifies to handle IOException, along with FileNotFoundException.
Unchecked exceptions
An unchecked exception is an exception that occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation.
For example, if you have declared an array of size 5 in your program, and trying to call the 6th element of the array then an ArrayIndexOutOfBoundsExceptionexception occurs.
Example
public class Unchecked_Demo { public static void main(String args[]) { int num[] = {1, 2, 3, 4}; System.out.println(num[5]); } }
If you compile and execute the above program, you will get the following exception.
Output
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at Exceptions.Unchecked_Demo.main(Unchecked_Demo.java:8)
- Related Articles
- Checked vs Unchecked exceptions in Java
- Checked Vs unchecked exceptions in Java programming.
- Checked vs Unchecked Exceptions in C#
- Are the instances of Exception checked or unchecked exceptions in java?
- What is the difference between checked and unchecked exceptions in Java?
- What are unchecked exceptions in Java?
- What are checked exceptions in Java?
- Difference Between Checked and Unchecked Exception in Java
- While chaining, can we throw unchecked exception from a checked exception in java?
- How can we decide that custom exception should be checked or unchecked in java?
- Can we change an exception of a method with throws clause from unchecked to checked while overriding it in java?
- Custom exceptions in Java
- Built-in Exceptions in Java
- How to handle the ArithmeticException (unchecked) in Java?
- How to handle the NumberFormatException (unchecked) in Java?
- How to handle the ArrayStoreException (unchecked) in Java?
