Java exception list
Java uses exceptions, like the majority of contemporary programming languages, to deal with both errors and "extraordinary events." When an exception arises inside the program, it messes up the regular logic of the instructions and abruptly ends the process.
Luckily, with a little planning and coding, you can frequently handle these exceptions graciously, enabling your code to keep operating and giving you information for identifying the reason for the unexpected outcome.
How the exceptions are handed
A class or function that encounters an exception generates the exception object and passes the data to that same runtime system (JVM).
The call stack is then traversed by the runtime system to ascertain which layer can manage an exception that was raised or thrown. The search starts with the method where exception was created and proceeds step-by-step through all the call stacks until an exception handler is located. It detects a match when the exception's type matches one that the exception handler can handle.
List of java exceptions with examples
Java defines exception kinds that are connected to its different class libraries. Java also gives users the option to create custom exceptions.

The Built-in Exceptions:
The built-in exceptions are the exceptions that exist in Java libraries. These exceptions can be used to explain a few different fault scenarios. The list of significant built-in Java exceptions is shown below.
- ArithmeticException: It is thrown whenever an exceptional circumstance occurs during an arithmetic operation.
- ArrayIndexOutOfBoundsException: It must be thrown to let the user know that an array was accessed using an improper index. Its index is either greater or equal to the array size, or it is negative.
- ClassNotFoundException: The classNotFoundException in Java arises when the JVM (Java Virtual Machine) tries to load a specific class but can't find the desired class in the classpath you gave, as the name implies. Your classpath is broken as a result of this (which is be a very common problem in the Java world). Beginners in Java may find this issue particularly perplexing. It must be caught or thrown to the caller because ClassNotFoundException is indeed a checked exception.
- FileNotFoundException: The java.io package also contains the exception class known as FileNotFoundException. The problem arises when we attempt to access a file that isn't installed on the system. It arises at run time rather than compile time, making it a checked exception.
- IOException: When performing input or output actions, such as reading a file or accessing a file, an IOException, or input-output exception, may arise. For instance, if we attempt to read a file using the incorrect path, we receive a FileNotFound Exception. Under the heading of the checked Exceptions, the IOExceptions fall. Exceptions that occur during the compilation of a Java programme are those that have been checked. FileNotFoundException, SSLException, and other subclasses of IOException are only a few examples.
- InterruptedException: The execution of any thread that is dozing off or waiting for anything can be interrupted by calling the interrupt() method, which displays an InterruptedException message. The interrupt() function of the Thread class can be used to stop a thread that would be dozing off or waiting.
- NoSuchFieldException: When the requested field (or variable) is missing from a class, it is thrown.
- NoSuchMethodException: When attempting to access a method that cannot be found, it is thrown.
- NullPointerException: Referencing the elements of a null objects causes an exception to be triggered. Null is a symbol for nothing.
- NumberFormatException: When a method tries to convert a string to a numeric representation but fails, this exception is thrown.
- RuntimeException: This shows an exception that happens in real time.
- StringIndexOutOfBoundsException: It is thrown by methods of the String class when such a value seems to be either negative or larger than the length of the string.
- IllegalArgumentException: When the method cannot be reached by the programme for a specific operation, an exception that would produce an error or error message. The unchecked exception applies to it.
Illustrations of the Built-in Exception
- Arithmetic exception
// The ArithmeticException in Java application
class ArithmeticException _Sahithi
{
public static void main(String args[])
{
try {
int s = 40, h = 0;
int r = s/h; // it cannot be divide by the zero
System.out.println ("Answer = " + r);
}
catch(ArithmeticException e) {
System.out.println (" it is a number tha cannot be divided by 0 ");
}
}
}
Output:

- NullPointer Exception
// NullPointerException example programme in Java
class NullPointer_Sahithi
{
public static void main(String args[])
{
try {
String h= null; //the value that is null
System.out.println(h.charAt(0));
} catch(NullPointerException e) {
System.out.println("NullPointerException");
}
}
}
OUTPUT:

- StringIndexOutOfBound Exception
// An example StringIndexOutOfBoundsException Java programme
class StringIndexOutOfBound_Sahithi
{
public static void main(String args[])
{
try {
String h = "The way she is smiling is very attractive "; // length is 42
char s = h.charAt(44); // here we are accessing the 45th element
System.out.println(s);
}
catch(StringIndexOutOfBoundsException e) {
System.out.println("…StringIndexOutOfBoundsException…");
}
}
}
Output :

- FileNotFound Exception
// Java example programme for the FileNotFoundException exception
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
class Sahithi {
public static void main(String args[]) {
try {
// the file which is in the following does not exist
File file = new File("E://file.txt");
FileReader sr = new FileReader(file);
} catch (FileNotFoundException e) {
System.out.println("No such file exists");
}
}
}
Output:

- IllegalArgumentException: The eligibility of a person to vote is determined by this programme. There won't be any errors thrown if the age is above or equal to 18. An error with an error statement will be generated if the aged is less than 18.
Additionally, "throw new IllegalArgumentException()" can be specified without an error message. In the IllegalArgumentException() function, we may optionally specify Integer.toString(variable name), which will print the name of the argument that does not meet the specified criterion.
/*whatever may be the package// exclude the package name from this text */
import java.io.*;
class Sahithi {
public static void print(int a)
{
if(s>=18){
System.out.println("Voting Rights Eligible");
}
else{
throw new IllegalArgumentException("Unqualified to Vote");
}
}
public static void main(String[] args) {
Kamal.print(15);
}
}
Output:
Exception in thread "main" java.lang.IllegalArgumentException: Unqualified to Vote at Kamal.print(File.java:14)
at Kamal.main(File.java:20)
The user defined exceptions
Java's built-in exceptions may occasionally be unable to adequately explain a particular circumstance. In certain circumstances, the user may also produce what are known as "user-defined Exceptions."
The steps for creating a user-defined exception are as follows.
- An exception class that is a subclasses of an Exception that class should be created by the user. Given that every exception is a subclass of an Exception that subclass, the user should do the same for his class. This is accomplished as follows:
class OwnException extends Exception
- It is possible to create a default constructor for the exception class.
OwnException(){}
- Another option is to create a constructor with parameters that takes a string.
This can be used to store exception information. We can use this to call the superclass(Exception) constructor and pass the string along.
OwnException(String sr)
{
super(sr);
}
- It is necessary to generate an object that belongs to the user-defined exception class and toss it through the throw clause in order to invoke an exception of that type, as in:
OwnException sr = new OwnException(“details of the exception”);
throw sr;
- How to construct your own exception class, MyException, as demonstrated in the application below.
- Three arrays are used to store information about account numbers, client names, and balance amounts.
- A for-loop is used in the main() method to display the details. At this point, it is checked to see if the balance in any account falls below the required minimum balance.
- If this is the case, then "Balance amount is less" is displayed and MyException is raised.
Let us have a look at to the following example
// Programming in Java to display a user-defined exception
// When balance is violated, this software throws an exception.
// the below given amount is Rs 20000
class OwnException extends Exception
{
//account information is stored
private static int acc[] = {2002, 2003, 2004, 2005};
private static String iden[] =
{"Hymavathi", "Janardhan", "Swetha", "Archana", "Sahithi"};
private static double mon[] =
{20000.00, 22000.00, 3600.0, 899.00, 4100.55};
// it is the default constructor
OwnException() { }
// it is the parameterized constructor
OwnException(String sr) { super(sr); }
// the main() is written here
public static void main(String[] args)
{
try {
// display the table's heading
System.out.println("ACC" + "\t" + "CUST" +
"\t" + "MON");
// show the exact account details
for (int k = 0; k < 5 ; k++)
{
System.out.println(acc[k] + "\t" + iden[k] +
"\t" + mon[k]);
// show your own exception in case of balance < 20000
if (mon[k] < 20000)
{
OwnException sh =
New OwnException("Balance falls short of 20000");
throw sh;
}
}
} //it is the end of try
catch (OwnException e) {
e.printStackTrace();
}
}
}
Here the runtime error is
OwnException: Balance falls short of 20000
at OwnException.main(fileProperty.java:36)
Output:
ACC CUST MON
2002 Hymavathi 20000.0
2003 Janardhan 22000.0
2004 Swetha 3600.0
2005 Archana 899.0