Handling Exceptions in Java

Common Java Exceptions

In Java, exceptions are events that disrupt the normal flow of a program. Understanding these exceptions is crucial for debugging and writing robust code. Below, we’ll explore 10 common Java exceptions, each with a brief explanation and a simple example.

1. NullPointerException

Occurs when trying to use an object reference that has not been initialized.
public class NullPointerExceptionDemo { public static void main(String[] args) { String str = null; System.out.println(str.length()); // Throws NullPointerException } }

2. ArrayIndexOutOfBoundsException

Thrown when attempting to access an array element with an invalid index.
public class ArrayIndexOutOfBoundsExceptionDemo { public static void main(String[] args) { int[] numbers = {1, 2, 3}; System.out.println(numbers[3]); // Throws ArrayIndexOutOfBoundsException } }

3. ArithmeticException

Occurs when an exceptional arithmetic condition has occurred, such as division by zero.
public class ArithmeticExceptionDemo { public static void main(String[] args) { int result = 10 / 0; // Throws ArithmeticException } }

4. ClassCastException

Thrown when an attempt is made to cast an object to a subclass of which it is not an instance.
public class ClassCastExceptionDemo { public static void main(String[] args) { Object obj = new Integer(100); String str = (String) obj; // Throws ClassCastException } }


5. IllegalArgumentException
Thrown to indicate that a method has been passed an illegal or inappropriate argument.
public class IllegalArgumentExceptionDemo { public static void main(String[] args) { setAge(-5); // Throws IllegalArgumentException }
public static void setAge(int age) { if (age < 0) { throw new IllegalArgumentException("Age cannot be negative"); } } }

6. NumberFormatException

Thrown to indicate that an attempt to parse a string to a number has failed.
public class NumberFormatExceptionDemo { public static void main(String[] args) { String str = "abc"; int number = Integer.parseInt(str); // Throws NumberFormatException } }

7. IndexOutOfBoundsException

Thrown to indicate that an index of some sort is out of range.
public class IndexOutOfBoundsExceptionDemo { public static void main(String[] args) { String str = "hello"; char ch = str.charAt(10); // Throws IndexOutOfBoundsException } }

8. IOException

Signals that an I/O exception of some sort has occurred.
import java.io.File; import java.io.FileReader; import java.io.IOException;
public class IOExceptionDemo { public static void main(String[] args) { File file = new File("nonexistentfile.txt"); try { FileReader fr = new FileReader(file); } catch (IOException e) { e.printStackTrace(); // Prints IOException } } }

9. FileNotFoundException

Thrown when an attempt to open the file denoted by a specified pathname has failed.
import java.io.File; import java.io.FileReader; import java.io.FileNotFoundException;
public class FileNotFoundExceptionDemo { public static void main(String[] args) { File file = new File("nonexistentfile.txt"); try { FileReader fr = new FileReader(file); } catch (FileNotFoundException e) { e.printStackTrace(); // Prints FileNotFoundException } } }

10. ClassNotFoundException

Thrown when an application tries to load a class through its string name but no definition for the class with the specified name could be found.
public class ClassNotFoundExceptionDemo { public static void main(String[] args) { try { Class.forName("com.nonexistent.ClassName"); } catch (ClassNotFoundException e) { e.printStackTrace(); // Prints ClassNotFoundException } } }
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.