Re: Use of AssertionError
- From: Mark Thornton <mark.p.thornton@xxxxxxxxxxxxxxxxxx>
- Date: Sun, 30 Dec 2007 21:44:59 GMT
Joshua Cranmer wrote:
Having just read the NullPointerException-IllegalArgumentException-AssertionError thread, I was just wondering if my use of AssertionError in one case was valid. This is a very pared-down interface of the code:
// Edited for simplicity
public static void getClass(String name) {
for (SourceHandler handler : handlers) {
if (handler.hasClass(name))
return; // Use this handler as the class
}
// Since our last handler claims to handle everything, we should
// never get here.
throw new AssertionError();
}
}
Where I throw the assertion error explicitly, I want to put an `assert false;' statement in (with a comment, of course), but then I would need to insert a `return null;' at the end of the method, which would invalidate the method contract which forbids null as a result but assertions may not be on.
Is it more appropriate to put a RuntimeException or other type of error instead of using an AssertionError here?
If it ever did happen you would be annoyed that the error message didn't include the name for which no handler was found. So at least pass 'name' to the constructor.
Mark Thornton
.
- References:
- Use of AssertionError
- From: Joshua Cranmer
- Use of AssertionError
- Prev by Date: Re: New File with a Directory
- Next by Date: Re: Use of AssertionError
- Previous by thread: Use of AssertionError
- Next by thread: Re: Use of AssertionError
- Index(es):
Relevant Pages
|