• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Assert

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will be the result of attempting to compile and run the following code fragment.



My test is New_value=1. So, the assert expression "i++ == 1" will be evaluated to true, therefoe AssertionError won't be throw.

But in the explanation on the whizlabs is New_value=3 follwed by an exception message.

Where is my mistake?

Thanks




 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the expression



assert 0==1 will be tested and result is a false so assertion error will be thrown followed by increment of i in assert i++ ==1 will make i as 1 and statement "Now " + ++i will make i as 2.After the finally statement will make i as 3
 
Ale Lima
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't see it.

Because when this line was executed we increased the variable i, therefore assert 0 == 1 that is false, after that i = 1, and when the finally block show the value we increased again. I am confused now.

 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When assert statement the value of i=0 so assertion is false.
After the test the value of i is one.. the value again gets incremented in the 2nd expression of assertion.
The Assertionerror is not caught .
And in finally it is again incremented to 3 before printing.
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I read it,

Following the assertion (i++ == 1), the pre increment operator is run ++i which makes i now equal to 1. Then in the catch block i is incremented again with the post increment operator ++i so i now equals 2. Finally, in the Finally block, i is incremented by another preincrement operator and thus equals 3. Since the Finally block runs as soon as the catch statement has completed it prints the output "New_value" 3, and then the AssertionError is printed to the console.


Does this help?



 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you don't specify any options when running java command, the default behavior is use java -da and you will get 1 as a result. If you use java -ea option, your code will get into the catch block and get 3 as a result.
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jia Tan,

Well remembered Thats certainly one reason for confusion!
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there is still some confusion going on.

With assertions enabled, the assertion will fail, and i will be incremented twice to 2 (first in the assertion condition, second in the assertion value expression.) An AssertionError will be thrown, but this won't get caught (since an AssertionError is not an Exception.) However, finally will run and increment i again to 3, output this value, and then the uncaught AssertionError will cause the program to terminate.
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah that makes sense, why the error is appearing after the finally statement. Its also obvious if we look at the stack trace, the error pops up in main, meaning it wasn't caught and passed down the stack till main 'blew-up!'


 
Jia Tan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My apologies, Ruben is right on this, it's not an Exception so won't be caught.
 
Ale Lima
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't specify anything when I running this code, especially because I executed inside of the netbeans.

Thank you very much, now everything makes sense.
 
reply
    Bookmark Topic Watch Topic
  • New Topic