• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

assert statement

 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I wrote this sample program to test the assert keyword and it's not giving me any AssertionError exception? I am using jdk1.5 btw.Please tell me where am going wrong.



Thanks.

[edit]Add code tags. CR[/edit]
[ September 01, 2008: Message edited by: Campbell Ritchie ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not going wrong at all. That is what an "assert" is supposed to do.
[ September 01, 2008: Message edited by: Campbell Ritchie ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and please find the Code button.
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but, assert a==11 is false right? a is assigned to only 10. so it should throw out an exception?

Sorry about the code tags.. I will paste my code in the tags from now on.

Thanks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The assertion tells the runtime to test that the asserted statement is correct. If the statement is incorrect (ie computes as false), then it throws the Error.

It's not actually an Exception, but an Error, because people usually catch Exception and not Error; this allows the Error to propagate so you know to check your code.
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run the above code, I am not getting any error messages too. Should I be getting an error message?

Thanks.
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe am not getting what you are saying properly. If you can, can you please explain it with a code example?

Thanks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you running it normally? You are supposed to say
java -ea AssertionTest

Assertions are disabled by default; you need the -ea flag (please check here for more details).
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I was running em without the -ea thing. Thanks a lot.. I see the error now.

Thanks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arjun Reddy:
Thanks a lot.
You're welcome. Sorry I didn't realise that was the problem earlier.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic