In this article we will show you the solution of list of checked and unchecked exceptions in java, checked exceptions are exceptions that are verified at compilation time by the Java compiler itself and are not included in the runtime exception class hierarchy.
In a programme, if a method throws a checked exception, it must either handle the exception itself or send it to the calling function.
Either by utilising a try-and-catch block or a throws clause in the method declaration, we must handle checked exceptions. If handled improperly, a compile-time error will result.
Step By Step Guide On List Of Checked And Unchecked Exceptions In Java :-
Below is the list of some important checked exceptions is given below:
- ClassNotFoundException
- InterruptedException
- InstantiationException
- IOException
- SQLException
- IllegalAccessException
- FileNotFoundException
Unchecked exceptions in Java are ones that the JVM, not the Java compiler checks. They take place when a software is running.
Runtime exceptions, also known as unchecked exceptions in Java, are all exceptions that fall within the runtime exception class.
We are able to create and compile a Java programme. However, until the programme is run, we cannot know the impact of unchecked exceptions and errors.
This is possible because the Java compiler enables us to create Java programmes without having to deal with unchecked errors and exceptions.
Whether the programmer manages a runtime exception or not is not checked by the Java compiler at compile time.
If a programmer fails to handle a runtime exception that occurs in a method, JVM will terminate the programme before the remaining code has had a chance to run.
Below examples of some unchecked exceptions :-
- ArithmeticException
- ClassCastException
- NullPointerException
- ArrayIndexOutOfBoundsException
- NegativeArraySizeException
- ArrayStoreException
- IllegalThreadStateException
- SecurityException, etc.
// Java Program to Illustrate Checked Exceptions // Where FileNotFoundException occurred // Importing I/O classes import java.io.*; // Main class class GFG { // Main driver method public static void main(String[] args) { // Now Reading file from path in local directory FileReader file = new FileReader("C:\\test\\a.txt"); BufferedReader fileInput = new BufferedReader(file); // Now Printing first 3 lines of file "C:\test\a.txt" for (int counter = 0; counter < 3; counter++) System.out.println(fileInput.readLine()); // Closing file connections // using close() method fileInput.close(); } } // Java Program to Illustrate Un-checked Exceptions // Main class class GFG { // Main driver method public static void main(String args[]) { // because it is mathematically incorrect int x = 0; int y = 10; int z = y / x; } }
- Importing I/O classes in Java
- Importing Main driver method
- Next step, reading file from path in local directory using FileReader file = new FileReader("C:\\test\\a.txt");
- Creating object as one way of taking input by using for loop iteration as well.
- Next step, printing first three lines of file
- Closing connections using fileInput.close(); method.
- Run the code
- Creating Main Class
- Creating the Main method
- Dividing the number by zero since it is mathematically wrong
- Run the code
Conclusion :-
So, in this tutorial we studied about the list of checked and unchecked exceptions in Java with two example programs of each.
I hope this article on list of checked and unchecked exceptions in java helps you and the steps and method mentioned above are easy to follow and implement.