• Post Reply Bookmark Topic Watch Topic
  • New Topic

AssertionError ignored in a method  RSS feed

 
manuel aldana
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, i am working with Assertions, amongst of them is Assert.codeNotReached:




no my problem is, that a method in which i place this Assertion ignores the throw of this exception and wants me to return a value:



that is really weird. i could place just a return null; after this assertion but that looks stupid and makes no sense.

any ideas why Java does not allow this?

thanks.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure this function's signature is void ?
 
manuel aldana
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
autsch!!

sorry, of course it returns something:

public returnType anyMethod(){
...
}
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If Assertions are disabled, nothing is thrown.
Nothing is returned.
Error.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stefan Wagner:
If Assertions are disabled, nothing is thrown.
Nothing is returned.
Error.


That's not true. He isn't using the assert keyword (which would make that statement true), but directly throwing the error.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by manuel aldana:

any ideas why Java does not allow this?


The compiler doesn't know that the method *always* throws the Error.

In fact, he *can't* know, because in Java you could later change the Assert class to not always throwing the error, without recompiling the client class.

What we are doing at work is directly throwing a dedicated RuntimeException at such places. We also sign the exception with our initials - which can be quite embarassing when the exception actually gets thrown in the production environment...

 
manuel aldana
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
alright, that makes sense, that compilation environment cannot know or won't analyze, that an exception gets thrown always.

by the way (you mentioning RuntimeException) it quite annoys me that there are so many checked Exceptions in java. sometimes it makes sense but most of the time it just makes code clumsy, longer and more difficult to read

often i build wrappers to "uncheck" some exception but that is annoying repetetive work too...
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!