• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions

 
TimD Moore
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
TimD Moore
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also,
Can you throw Error or Runtime exceptions?
Thanks,
TM
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Priyanka Chopda
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
TimD Moore
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
TM
 
Davide Zanichelli
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Davide Zanichelli
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic