File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Some puzzles in S&B on assertions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Some puzzles in S&B on assertions " Watch "Some puzzles in S&B on assertions " New topic
Author

Some puzzles in S&B on assertions

Graeme Byers
Ranch Hand

Joined: Apr 16, 2004
Posts: 127
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

Joined: Jun 09, 2008
Posts: 117
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.


Java hobbyist.
Graeme Byers
Ranch Hand

Joined: Apr 16, 2004
Posts: 127
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Some puzzles in S&B on assertions