All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

List Of Checked And Unchecked Exceptions In Java

Last Updated : Mar 11, 2024

List Of Checked And Unchecked Exceptions In Java

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;
}
}
  1. Importing I/O classes in Java
  2. Importing Main driver method
  3. Next step, reading file from path in local directory using FileReader file = new FileReader("C:\\test\\a.txt");
  4. Creating object as one way of taking input by using for loop iteration as well.
  5. Next step, printing first three lines of file
  6. Closing connections using fileInput.close(); method.
  7. Run the code
  8. Creating Main Class
  9. Creating the Main method
  10. Dividing the number by zero since it is mathematically wrong
  11. 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.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪