VOOZH about

URL: https://www.geeksforgeeks.org/java/types-of-exception-in-java-with-examples/

⇱ Types of Exception in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Types of Exception in Java with Examples

Last Updated : 1 Jun, 2026

Exceptions in Java are events that occur during program execution and disrupt the normal flow of a program. Java provides many built-in exceptions to handle common runtime and compile-time errors. Developers can also create custom exceptions to handle application-specific error conditions.

  • Exceptions help detect and handle errors gracefully.
  • Java provides both built-in and user-defined exceptions.
  • Proper exception handling improves program reliability and maintainability.

👁 exceptions-in-java

Built-in Exceptions

Built-in exceptions are predefined exception classes provided by Java's standard library to handle common runtime and compile-time errors. They help identify and manage abnormal situations such as invalid input, file errors, arithmetic errors, and null references. Java automatically throws these exceptions when such conditions occur.

  • Built-in exceptions are predefined and available in Java libraries.
  • They help handle common errors and prevent unexpected program termination.

Examples of Built-in Exception

1. ArithmeticException

ArithmeticException occurs when an illegal arithmetic operation is performed, such as division by zero.

  • Commonly occurs during mathematical calculations.
  • It is an unchecked exception.

Output
Can't divide a number by 0

2. NullPointerException

NullPointerException occurs when a program tries to access a method or variable using a null reference.

  • One of the most common runtime exceptions.
  • Occurs when an object reference points to null.

Output
NullPointerException..

3. StringIndexOutOfBoundsException

StringIndexOutOfBoundsException occurs when an invalid index is used to access characters in a string.

  • Happens when index is less than 0 or greater than string length.
  • Thrown by String class methods.

Output
StringIndexOutOfBoundsException

4. FileNotFoundException

FileNotFoundException occurs when a specified file cannot be found or opened.

  • Common in file handling operations.
  • It is a checked exception.

Output:

File does not exist

5. NumberFormatException

NumberFormatException occurs when a string cannot be converted into a numeric value.

  • Commonly occurs with parsing methods.
  • It is an unchecked exception.

Output
Number format exception

6. ArrayIndexOutOfBoundsException

ArrayIndexOutOfBoundsException occurs when an invalid array index is accessed.

  • Index is either negative or exceeds array size.
  • It is an unchecked exception.

Output
Array Index is Out Of Bounds

7. IOException

IOException occurs when an input/output operation fails during file or stream processing.

  • Related to reading and writing data.
  • It is a checked exception.

Output:

Hello Geek! 
Exception Output: null

8. NoSuchMethodException

NoSuchMethodException occurs when a requested method does not exist.

  • Common in Reflection API.
  • It is a checked exception. 

9. IllegalArgumentException

IllegalArgumentException occurs when a method receives an invalid argument.

  • Indicates inappropriate method parameters.
  • It is an unchecked exception.

Output : 

Exception in thread "main" java.lang.IllegalArgumentException: Not Eligible for Voting
at GFG.print(File.java:13)
at GFG.main(File.java:19)

10. IllegalStateException

IllegalStateException occurs when a method is invoked at an inappropriate time or state.

  • Indicates the object is not in the required state.
  • It is an unchecked exception.

Output :

Exception in thread "main" java.lang.IllegalStateException: Either one or two numbers are not Positive Integer
at GFG.main(File.java:20)

11. ClassNotFoundException

ClassNotFoundException occurs when the JVM cannot find a specified class at runtime.

  • Common when loading classes dynamically.
  • It is a checked exception.

Output
java.lang.ClassNotFoundException: Class1
Class Not Found...

User-Defined Exceptions

User-defined exceptions are custom exceptions created by programmers to handle application-specific error conditions that are not covered by Java's built-in exceptions.

  • Created by extending the Exception class.
  • Used to represent custom business logic errors.

Syntax:

class MyException extends Exception {
MyException() { }
MyException(String message) {
super(message);
}
}

Throwing a User-Defined Exception:

MyException me = new MyException("Exception details");
throw me;

Example

Runtime Error 

 MyException: Balance is less than 1000
at MyException.main(fileProperty.java:36)

Output:

ACCNO CUSTOMER BALANCE
1001 Nish 10000.0
1002 Shubh 12000.0
1003 Sush 5600.0
1004 Abhi 999.0
Comment
Article Tags:
Article Tags: