Banu Chowdary wrote:To discourage you from trying
to substitute an assertion for an exception, the AssertionError doesn't provide
access to the object that generated it. All you get is the String message. <From Chapter 5 K&B>
Can anyone explain me what's the point in this???
What happens if we catch an assertionError using try
OUTPUT : haijava.lang.AssertionError: helloAssertError // didn't understand how it comes.......
AssertionError does not allow you to pass the cause of the error. If you look at the constructors, there only allow you to pass a message.
In Error(API)..we have.
Error(String message, Throwable cause)
Constructs a new error with the specified detail message and cause.
Error(Throwable cause)
Constructs a new error with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause).
There is no constructor like above in AssertionError..
In you code, you are using second version of assertion
assert Expression1 : Expression2 ;
if Expression1 is false, its supposed to throw AssertionError. Expression 2 is used as a message that you are getting as output.