Home  >  Article  >  Java  >  Deep Dive into Java Exceptions

Deep Dive into Java Exceptions

PHPz
PHPzOriginal
2024-08-19 06:01:02458browse

Java Core Exception Class

hierarchy of exception classes: Throwable < Error < Exception where the Throwable is the superclass

Throwable

The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. reference

In practice, Throwable is not typically used directly by developers. Instead, it serves as the foundation for its two direct subclasses: Error and Exception.

try {
 // some process
} catch (Throwable t) {
    throw new Throwable(t);
}

Error

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Errors typically represent abnormal conditions that occur in the JVM itself reference

An abnormal condition means that the problem typically arises from factors outside the control of the application and is generally unrecoverable. Ex : OutOfMemoryError, StackOverflowError

Deep Dive into Java Exceptions

Exception

Exception refers to an unexpected event or condition that occurs during the execution of a program, and we should try catch. By catching exceptions we should be able to handle unexpected situations gracefully, ensuring that the program doesn't crash.

int[] arr = {1, 2, 3};
try {
    System.out.println(arr[3]); // This will throw ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException e) {
    System.out.println("Unchecked Exception: " + e.getMessage());
}

 

Types of Exceptions

There are 2 types of exception in java. reference

Checked Exceptions

Checked exception is like a situation that you know might go wrong, so you're required to plan for it. They must either be caught using a try-catch block or be declared in the method signature with a throws clause. If a method can throw a checked exception and you don't handle it, the program will not compile.

// Checked exception
try {
    readFile("nonexistent.txt");
} catch (FileNotFoundException e) {
    System.out.println("Checked Exception: " + e.getMessage());
}
// Checked exception
public void getData() throws SQLException { // throw SQLException
    throw new SQLException("err");
}

Unchecked Exception

Unchecked exceptions, aka. runtime exceptions, are exceptions that the Java compiler does not require you to handle. They are subclasses of RuntimeException. Unlike checked exceptions, these exceptions do not need to be caught or declared in the method signature. They usually indicate programming errors, such as logic flaws, incorrect use of APIs, or violations of assumptions in the code.

String text = null;
System.out.println(text.length()); // This will throw a NullPointerException

 

Common Types of Java Exceptions

NullPointerException: Occurs when an application tries to use an object reference that has not been initialized.

String text = null; // text is not initialized
try {
    System.out.println(text.length()); // Attempting to call length() on a null reference
} catch (NullPointerException e) {
    System.out.println("Caught a NullPointerException: " + e.getMessage());
}

ArrayIndexOutOfBoundsException: Thrown when an attempt is made to access an array with an illegal index.

int[] numbers = {1, 2, 3};
try {
    int value = numbers[5]; // Attempting to access index 5, which is out of bounds
} catch (ArrayIndexOutOfBoundsException e) {
    System.out.println("Caught an ArrayIndexOutOfBoundsException: " + e.getMessage());
}

IllegalArgumentException: Thrown when a method receives an inappropriate argument.

public class IllegalArgumentExample {
    public static void setAge(int age) {
        if (age < 0) {
            throw new IllegalArgumentException("Age cannot be negative"); // Illegal argument
        }
        System.out.println("Age set to: " + age);
    }

    public static void main(String[] args) {
        try {
            setAge(-5); // Passing a negative age, which is illegal
        } catch (IllegalArgumentException e) {
            System.out.println("Caught an IllegalArgumentException: " + e.getMessage());
        }
    }
}

 

How To Handle Exceptions

Try-Catch

Use try-catch when you want to handle specific exceptions that might be thrown within a block of code

try {
    int result = 10 / 0; // This will throw ArithmeticException
} catch (ArithmeticException e) {
    System.out.println("Caught an ArithmeticException: " + e.getMessage());
}

Multi-Catch

Use multi-catch when you want to handle multiple exception types in a similar manner

try {
    String str = null;
    System.out.println(str.length()); // This will throw NullPointerException
} catch (NullPointerException | ArrayIndexOutOfBoundsException e) {
    System.out.println("Caught an exception: " + e.getMessage());
}

Try-With-Resources

Use try-with-resources when you are working with resources that need to be closed after use, such as files, sockets, or database connections.

try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
} catch (IOException e) {
    System.out.println("Caught an IOException: " + e.getMessage());
}

Finally

Use the finally block when you need to ensure that certain code executes regardless of whether an exception was thrown or not

try {
    file = new FileReader("file.txt");
} catch (IOException e) {
    System.out.println("Caught an IOException: " + e.getMessage());
} finally {
    if (file != null) {
        file.close(); // Ensure the file is closed
    }
}

 

Best Practices for Exception Handling

Do Not Ignore Exceptions: exceptions should be handled appropriately, not just caught and ignored.

try {
    file = new FileReader("file.txt");
} catch (IOException ignored) {
    // ignored
}

Use Specific Exceptions: use specific exceptions rather than using generic ones.

try {
    // Code that may throw exceptions
    String text = null;
    text.length();
} catch (Exception e) {
    // Too broad; will catch all exceptions
    System.err.println("An error occurred: " + e.getMessage());
}

proper way to handle it:

try {
    // Code that may throw exceptions
    String text = null;
    text.length();
} catch (NullPointerException e) {
    // Handle specific exception
    System.err.println("Null pointer exception: " + e.getMessage());
} catch (Exception e) {
    // Handle other exceptions
    System.err.println("An error occurred: " + e.getMessage());
}

Clean Resource Handling: always close resources to avoid memory leaks

FileReader fileReader = null;
try {
    fileReader = new FileReader("file.txt");
    // Read from the file
} catch (IOException e) {
    System.err.println("File not found: " + e.getMessage());
} finally {
   fileReader.close(); // clouse resources
}

Custom Exceptions: create custom exceptions when the standard exceptions don’t suit the specific error conditions.

// Custom Exception
public class CustomException extends Exception {
    public CustomException(String message) {
        super(message);
    }
}

// Usage
public class Example {
    public void performOperation() throws CustomException {
        // Some condition
        throw new CustomException("Custom error message");
    }

    public static void main(String[] args) {
        Example example = new Example();
        try {
            example.performOperation();
        } catch (CustomException e) {
            System.err.println("Caught custom exception: " + e.getMessage());
        }
    }
}

Logging: log exceptions for debugging and maintenance.

public class Example {
    private static final Logger logger = Logger.getLogger(Example.class.getName());

    public void riskyMethod() {
        try {
            // Code that may throw an exception
            int result = 10 / 0;
        } catch (ArithmeticException e) {
            // Log the exception
            logger.severe("An arithmetic error occurred: " + e.getMessage());
        }
    }
}

Avoid Overusing Exceptions: Warn against using exceptions for controlling flow; they should only be used for handling truly exceptional situations.

public class Example {
    public void process(int[] array) {
        try {
            // Using exceptions to control flow
            if (array.length < 5) {
                throw new ArrayIndexOutOfBoundsException("Array too small");
            }
            // Process array
        } catch (ArrayIndexOutOfBoundsException e) {
            // Handle exception
            System.out.println("Handled array size issue.");
        }
    }
}

proper way to handle it:

public class Example {
    public void process(int[] array) {
        if (array.length >= 5) {
            // Process array
        } else {
            // Handle the condition without using exceptions
            System.out.println("Array is too small.");
        }
    }
}




 
any feedback will be helpful :)

The above is the detailed content of Deep Dive into Java Exceptions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn