aspose file tools*
The moose likes Beginning Java and the fly likes assert statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "assert statement" Watch "assert statement" New topic
Author

assert statement

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 629
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 ]

Be Humble... Be Nice.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40029
    
  28
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

Joined: Oct 13, 2005
Posts: 40029
    
  28
. . . and please find the Code button.
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 629
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

Joined: Oct 13, 2005
Posts: 40029
    
  28
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

Joined: Nov 10, 2007
Posts: 629
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

Joined: Nov 10, 2007
Posts: 629
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

Joined: Oct 13, 2005
Posts: 40029
    
  28
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

Joined: Nov 10, 2007
Posts: 629
Yeah I was running em without the -ea thing. Thanks a lot.. I see the error now.

Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40029
    
  28
Originally posted by Arjun Reddy:
Thanks a lot.
You're welcome. Sorry I didn't realise that was the problem earlier.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: assert statement