Catching specific and general exceptions

Report a typo

You are developing a Java application that needs to handle thrown exceptions. In your current piece of code, there's a method named 'throwException' which throws an 'ArithmeticException'. If this exception occurs, you want to catch it specifically and print a custom message. Apart from this, if any other exception arises, you should still be able to catch it and print a different message. Fill in the blanks in the provided code snippet to ensure that your application catches the 'ArithmeticException' as well as any other potential exceptions.

Fill in the gaps with the relevant elements
public class Main {
    public static void main(String[] args) {
         {
            throwException();
        }  (ArithmeticException e) {
            System.out.println("Caught ArithmeticException: " + e.getMessage());
        } catch ( e) {
            System.out.println("Caught Exception: " + e.getMessage());
        }
    }

    static void throwException()  Exception {
        throw new ArithmeticException("Division by zero");
    }
}
throwsExceptioncatchtry
___

Create a free account to access the full topic