This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi, Regarding exceptions, I realize you are only required to handle checked exceptions, and are not required to handle Runtime or Error exceptions. Is there anything wrong with handling the Runtime or Error exceptions though, it is illegal to do so? Thanks, TM
Joined: Aug 19, 2003
Also, Can you throw Error or Runtime exceptions? Thanks, TM
hi: Errors can occur at many points of the program, and recovery from them is almost impossible. Its subclass RuntimeException, are not checked by the compiler. It is legal to catch RuntimeExceptins. you can read more about it at chapter 11 of JLS Hanna
SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
hi, "subtypes of error or runtimeException are unchecked, so the compiler doesn't enforce the handle or declare rule. You're free to handle them, and you're free to declare them, but compiler doesn't care one way or the other." from K&B -PC
I know that usually it's quite unreasonable catch and try to recover from an error (OutOfMemoryError...), but i think it make sense to try to recover from an AssertionError, since if i'm using assertions to check the pre-conditions of a private method, i've more or less the same chances to recover as catching an InvalidArgumentException thrown from a public method...
Hi Davide Assertion Errors are not be handled. What if you turn off the assertion and your error recovery mechanism would fail then. I can't think of any practical situation where you should handle assertion error.
Joined: Aug 22, 2003
i just think that a general handler could catch this assertion errors, just to avoid crashing all the application when only some part of it could be closed and restarted. same as a global catch of exception would do for uncaught errors. and if you don't have assertions enabled, the handler would have simply no job what do you think?
Joined: Apr 13, 2003
Hi Davide In case we need to check the pre-conditions of a private method and you want to recover then I would suggest you use another error. I guess that would be more appropriate. [ August 29, 2003: Message edited by: Anupam Sinha ]