-2

It is clearly stated that unchecked exceptions are not supposed to be caught wherever I read about it. However, if an input is needed, everyone has always caught it in any code I've seen.

Considering it is an unchecked exception, how would you handle it? Would you prevent it in the first place? Why is nobody following the practise?

Haggra
  • 1,951
  • 1
  • 17
  • 27
  • 1
    What is your question? Is it *Why are people catching unchecked exception when you read you're not supposed to?* (also, please link to where you read that, and you can refer to http://stackoverflow.com/a/13251421/1743880), or is it *How do you handle invalid input* (and for that, refer to http://stackoverflow.com/questions/3059333/validating-input-using-java-util-scanner)? Those are 2 different questions (and both of them have been asked before) – Tunaki Aug 09 '16 at 12:34
  • Where did you read that? Unchecked exception are not always the result of programmer's error. It is more subtle and depends on the exception. Please refer to http://stackoverflow.com/questions/13251368/should-unchecked-exceptions-be-caught-and-dealt-with – Tunaki Aug 09 '16 at 12:41

1 Answers1

1

It is clearly stated that unchecked exceptions are not supposed to be caught

It not true that you should never catch unchecked exception.

If you can handle exception in reasonable way, you should catch. For example you can catch NumberFormatException (which is unchecked exception) and show message box for user to indicate that input is wrong.

In other hand you shouldn't catch Exceptions which are hard/impossible to recover. In this case the best strategy is to allow the application to exit.

You should also never catch Errors (like for example OutOfMemoryError) except rare cases.

Community
  • 1
  • 1
Krzysztof Atłasik
  • 18,129
  • 4
  • 42
  • 64