What is runtime exception in Java with example?

Miriam 219 Published: 11/16/2024

What is runtime exception in Java with example?

I'm glad you asked!

Runtime exceptions are types of exceptions that occur during the execution of a Java program, often due to invalid operations or unexpected situations. They are usually caused by errors that can be detected at runtime, such as null pointer exceptions, out-of-bounds array access, or incorrect type conversions.

Here's an example of a runtime exception in Java:

public class RuntimeExample {

public static void main(String[] args) {

try {

int[] array = {1, 2, 3};

System.out.println(array[5]); // Attempt to access out-of-bounds index

} catch (ArrayIndexOutOfBoundsException e) {

System.out.println("Caught an ArrayIndexOutOfBoundsException: " + e.getMessage());

}

}

}

In this example, we create an integer array with three elements. Then, we try to access the element at index 5, which is out of bounds. This will throw a ArrayIndexOutOfBoundsException. The try-catch block is used to catch and handle this exception.

When you run this code, you'll see the following output:

Caught an ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 3

Other examples of runtime exceptions in Java include:

NullPointerException: Thrown when you try to access or manipulate a null object. ClassCastException: Thrown when you try to cast an object to a class that it's not an instance of. ArithmeticException: Thrown when you perform an arithmetic operation that results in an undefined value, such as dividing by zero.

Runtime exceptions are important in Java because they allow your program to handle and recover from unexpected errors. By catching and handling these exceptions, you can ensure that your program remains robust and continues running even if unexpected situations occur.

In summary, runtime exceptions are types of exceptions that occur during the execution of a Java program, often due to invalid operations or unexpected situations. They're usually caught using try-catch blocks and handled by implementing custom error-handling logic.

List of runtime exceptions in Java

I'd be happy to help you with that! Here is the list of runtime exceptions in Java:

ArrayIndexOutOfBoundsException: Thrown when an array index is out of bounds.

ArithmeticException: Thrown when there's an error during arithmetic operations, such as division by zero.

ClassCastException: Thrown when attempting to cast an object to a class it doesn't belong to.

IllegalMonitorStateException: Thrown when trying to invoke a monitor method (like wait(), notify(), or notifyAll()) on an unlock-only monitor.

IllegalStateException: Thrown when the recipient of the message is not in the correct state for the request being made, or when an attempt is made to do something that doesn't make sense for that state.

IndexOutOfBoundsException: Thrown when attempting to access an array or vector with an index that's out of bounds.

NegativeArraySizeException: Thrown when trying to create an array with a negative size.

NullPointerException: Thrown when accessing a null object reference, such as calling null.toString().

OutOfMemoryError: Thrown when the Java virtual machine runs out of memory or can't allocate more space for your program's data structures and objects.

StackOverflowError: Thrown when the recursion level exceeds the maximum depth allowed.

StringIndexOutOfBoundsException: Thrown when attempting to access a string with an index that's out of bounds.

ThreadDeath: Thrown when a thread is being stopped (interrupted) and can't be interrupted anymore.

UnsupportedOperationException: Thrown when trying to use an interface method that's not implemented by the class implementing that interface.

UnknownHostException: Thrown when trying to connect to a host that can't be found or resolves to an unknown IP address.

IOException: Thrown when an input/output operation fails, such as reading or writing from/to a file stream.

SecurityException: Thrown when there's a security constraint violation, such as a permission denial or authentication failure.

NoSuchElementException: Thrown when attempting to access an element that doesn't exist in a collection (like a queue, stack, or list).

UnsupportedOperationException: Thrown when trying to use a method of an interface that the class implementing it doesn't support.

InterruptedException: Thrown when interrupted while performing an operation that's subject to interruption, like wait(), sleep(), or I/O operations.

Remember that these exceptions can be caught and handled with try-catch blocks in your Java code.