Java RuntimeException

Introduction

RuntimeException in Java is an unchecked exception that occurs during the execution of a program. It represents programming errors that can be caught by the program but are not required to be handled.

Table of Contents

  1. What is RuntimeException?
  2. Characteristics of RuntimeException
  3. Common Subclasses
  4. Best Practices
  5. Examples
  6. Conclusion

1. What is RuntimeException?

RuntimeException is a superclass of exceptions that can occur during the normal operation of the JVM. It is part of the unchecked exceptions and does not need to be declared or caught.

2. Characteristics of RuntimeException

  • Unchecked: Not required to be caught or declared in a method's throws clause.
  • Programming Errors: Represents errors such as logic flaws or improper use of APIs.
  • Inheritance: Extends java.lang.Exception.

3. Common Subclasses

  • NullPointerException: Accessing a method or property on a null object.
  • ArrayIndexOutOfBoundsException: Accessing an array with an illegal index.
  • ClassCastException: Invalid casting of an object.
  • IllegalArgumentException: Invalid arguments passed to a method.
  • ArithmeticException: Errors like division by zero.

4. Best Practices

  • Avoid Unnecessary Use: Use RuntimeException for unexpected errors only.
  • Catch Specific Exceptions: Catch specific subclasses rather than the general RuntimeException.
  • Input Validation: Prevent RuntimeException by validating inputs and handling possible errors.

5. Examples

Example 1: Catching NullPointerException

This example demonstrates how to handle a NullPointerException.

public class NullPointerExample {
    public static void main(String[] args) {
        try {
            String str = null;
            System.out.println(str.length());
        } catch (NullPointerException e) {
            System.out.println("Caught NullPointerException: " + e.getMessage());
        }
    }
}

Output:

Caught NullPointerException: Cannot invoke "String.length()" because "<local1>" is null

Example 2: Catching ArrayIndexOutOfBoundsException

This example shows how to handle an ArrayIndexOutOfBoundsException.

public class ArrayIndexExample {
    public static void main(String[] args) {
        try {
            int[] numbers = {1, 2, 3};
            System.out.println(numbers[5]);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Caught ArrayIndexOutOfBoundsException: " + e.getMessage());
        }
    }
}

Output:

Caught ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 3

Example 3: Using IllegalArgumentException

This example demonstrates throwing an IllegalArgumentException.

public class IllegalArgumentExample {
    public static void main(String[] args) {
        try {
            printAge(-5);
        } catch (IllegalArgumentException e) {
            System.out.println("Caught IllegalArgumentException: " + e.getMessage());
        }
    }

    public static void printAge(int age) {
        if (age < 0) {
            throw new IllegalArgumentException("Age cannot be negative.");
        }
        System.out.println("Age: " + age);
    }
}

Output:

Caught IllegalArgumentException: Age cannot be negative.

6. Conclusion

RuntimeException in Java is used for errors that occur during the program's runtime and do not require explicit handling. By following best practices and catching specific exceptions, developers can create more robust and error-resistant applications.

Comments