Java - Built-in Exceptions | Comprehensive Guide
Explore Java's built-in exceptions, their types, and examples of checked and unchecked exceptions with code snippets and explanations.
Java - Built-in Exceptions
Java provides several built-in exceptions through the java.lang
package. These exceptions are part of the standard Java libraries and help developers handle common error scenarios.
The most general exceptions in Java are subclasses of the RuntimeException
class. Since java.lang
is automatically imported in all Java programs, exceptions derived from RuntimeException
are readily available.
Types of Java Built-in Exceptions
Java built-in exceptions are broadly categorized into two types:
1. Checked Exceptions
Checked exceptions must be handled by the programmer during code writing. These exceptions occur during compile-time and can be managed using the try-catch
block.
2. Unchecked Exceptions
Unchecked exceptions occur at runtime and do not require explicit handling by the programmer. Some common unchecked exceptions include NullPointerException
, ArrayIndexOutOfBoundsException
, and ArithmeticException
.
Common Built-in Exceptions in Java
Java defines a wide range of exceptions for handling different error scenarios. Below is a list of commonly encountered exceptions:
Exception | Description |
---|---|
ArithmeticException |
Arithmetic error, such as division by zero. |
ArrayIndexOutOfBoundsException |
Array index is out of bounds. |
ArrayStoreException |
Assignment of an incompatible type to an array element. |
ClassCastException |
Invalid type casting. |
IllegalArgumentException |
Invalid argument passed to a method. |
IllegalMonitorStateException |
Illegal monitor operation, such as waiting on an unlocked thread. |
IllegalStateException |
Invalid state of the environment or application. |
IndexOutOfBoundsException |
An index is out of bounds. |
NullPointerException |
Invalid use of a null reference. |
NumberFormatException |
Invalid conversion of a string to a numeric format. |
SecurityException |
Security violation attempt. |
StringIndexOutOfBoundsException |
Attempt to access a string outside its valid range. |
UnsupportedOperationException |
Unsupported operation encountered. |
ClassNotFoundException |
Specified class not found. |
IllegalAccessException |
Access to a class is denied. |
Examples of Java Built-in Exceptions
Example 1: Demonstrating ArithmeticException
Without try-catch
In this example, dividing a value by 0 causes an ArithmeticException
. Since the exception is unchecked, it occurs at runtime and terminates the program.
Syntax
package com.tutorialsarena;
public class ExcepTest {
public static void main(String args[]) {
int b = 0;
int c = 1 / b; // Division by zero
System.out.println("c :" + c);
}
}
Output
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.tutorialsarena.ExcepTest.main(ExcepTest.java:7)
Example 2: Demonstrating ArithmeticException
With try-catch
In this example, we handle the ArithmeticException
using a try-catch block, which allows the program to continue after catching the exception.
Syntax
package com.tutorialsarena;
public class ExcepTest {
public static void main(String args[]) {
try {
int b = 0;
int c = 1 / b; // Division by zero
System.out.println("c :" + c);
} catch (ArithmeticException e) {
System.out.println("Exception thrown: " + e);
}
System.out.println("Out of the block");
}
}
Output
Exception thrown: java.lang.ArithmeticException: / by zero
Out of the block
Example 3: Demonstrating NoSuchMethodException
This example demonstrates a checked exception, which must either be handled or declared. The NoSuchMethodException
is thrown by the getName()
method, which needs proper handling or declaration to avoid compilation errors.
Syntax
package com.tutorialsarena;
public class ExcepTest {
public static void main(String args[]) {
ExcepTest excepTest = new ExcepTest();
excepTest.getName(); // Throws NoSuchMethodException
}
private String getName() throws NoSuchMethodException {
throw new NoSuchMethodException(); // Manually throwing the exception
}
}
Output
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unhandled exception type NoSuchMethodException
at com.tutorialsarena.ExcepTest.main(ExcepTest.java:7)