Sobes.tech
Junior — Middle

Is it possible to use multiple catch blocks in a single try statement?

sobes.tech AI

Answer from AI

Yes, in Java (and consequently in Android), you can use multiple catch blocks after a single try statement. Each catch block is designed to handle a specific type of exception. This allows for more precise error handling.

Example:

try {
    // code that may throw an exception
} catch (IOException e) {
    // handle IO errors
} catch (NullPointerException e) {
    // handle null pointer errors
} catch (Exception e) {
    // handle all other exceptions
}