Java Exceptions
Java Exceptions
Checked Exceptions
C throws
hecked exceptions must be handled (using try-catch or declared using ) during
compile-time.
IOException
Raised when an I/O operation fails or is interrupted.
Example:
QLException
S
Raised when a database access error occurs.
Example:
import java.sql.*;
public class SQLExceptionExample {
public static void main(String[] args) {
try {
Connection conn =
DriverManager.getConnection("invalid_url", "user", "password");
} catch (SQLException e) {
System.out.println("Database error: " + e.getMessage());
}
}
}
lassNotFoundException
C
Raised when a class cannot be found at runtime.
Example:
Unchecked Exceptions
ullPointerException
N
null
Raised when trying to access an object that is .
Example:
java
rrayIndexOutOfBoundsException
A
Raised when accessing an invalid index of an array.
Example:
rithmeticException
A
Raised when an illegal arithmetic operation occurs, such as division by zero.
Example:
j
umberFormatException
N
Raised when attempting to convert an invalid string to a number.
Example:
IllegalArgumentException
Raised when an illegal argument is passed to a method.
Example:
Errors (Rare and Serious Issues)
utOfMemoryError
O
Raised when the JVM runs out of memory.
Example:
tackOverflowError
S
Raised when the call stack overflows, usually due to deep or infinite recursion.
Example:
his list covers commonly encountered exceptions. Let me know if you need examples for
T
more specific scenarios!