Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Some puzzles in S&B on assertions

 
Graeme Byers
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. From S&B page 374
// the following six are legal assert statements
assert (x == 1) : new ValidAssert() ;
Well it compiles, but what is being returned? Constructors are void.
Nothing in JavaRanch errata. ValidAssert is not coded by S&B.
And although its illegal, what is the following even trying to do ?
assert (x == 1) : ValidAssert va ;
I can easily see what the other illegals are mistakenly attempting.

2. Why do people throw an IllegalArgumentException when say firstParm == secondParm ?
The name is appropriate but is its behaviour - it's unchecked so should not a programmer be forced to declare that his method does throw an exception suitable for this error ?
 
Ronald Schild
Ranch Hand
Posts: 117
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. From S&B page 374
// the following six are legal assert statements
assert (x == 1) : new ValidAssert() ;
Well it compiles, but what is being returned? Constructors are void.
Nothing in JavaRanch errata. ValidAssert is not coded by S&B.


The resulting value of the second expression is used as information in the stack trace of the exception that is thrown when the assertion fails. It may be any value, just as:


x may be any value. In the above example, an object of class ValidAssert is instantiated which is then used to add information. I'm not aware of the exact underlying mechanism, but I bet my 2ct that the object's toString() method is called eventually.

And although its illegal, what is the following even trying to do ?
assert (x == 1) : ValidAssert va ; I can easily see what the other illegals are mistakenly attempting.


It is trying to point out what not to do. The expression does not result in a value, rather it declares a reference that is not initialized and so does not result in a proper value.

Consider the code:



2. Why do people throw an IllegalArgumentException when say firstParm == secondParm ?
The name is appropriate but is its behaviour - it's unchecked so should not a programmer be forced to declare that his method does throw an exception suitable for this error ?


Is your question: Why isn't IllegalArgumentException checked? I was a bit unsure as well, but I found a bit of info here. It helped me, maybe it has the answer you seek? If not, do not hesitate to ask more.
 
Graeme Byers
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ronald,
I did realise that an object would be returned, but I expected something interesting to happen not for the problem to be silently ignored.
I also read JavaPractices before posting - they are "some people" - see under assertions. It's just odd like RunTimeException being an Exception.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic