2

Scenario:

class Assert {
    public static void main(String []args) {
        try {
            assert false;
        }
        catch (RuntimeException re) {
            System.out.println("In the handler of RuntimeException");
        }
        catch (Exception e) {
            System.out.println("In the handler of Exception");
        }
        catch (Error ae) {
            System.out.println("In the handler of Error");
        }
        catch (Throwable t) {
            System.out.println("In the handler of Throwable");
        }
    }
}

I am expecting 'In the handler of Error' because AssertionError is subclass of Error but it doesn't show anything and terminate normal. after then to check the out put I added this one catch handler before Error handler .

catch (AssertionError t) {
    System.out.println("In the handler of Throwable");
}

in know it's not a good practice to catch Error but if we does not need to catch why the program was not crashed it terminate normally?

1 Answer 1

2

By default assertions are disabled, add -ea in the command line when you execute your code with java:

java -ea Assert
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.