0

I am just writing a code and want to know whether it is good to use exception class to catch an exception or using multiple type of try-catch.

try{
    // some error-prone code
   }catch(Exception e){
   }

or,

try{
    // some error-prone code
   }catch(NullPointerException n){
   }catch(ArrayOutOfBoundException a){
   } ..... etc
ASK
  • 1,136
  • 1
  • 10
  • 14
  • 1
    If you want handle each type of exception differently then use multiple 'catch' else one is enough. – Gopi Feb 19 '19 at 04:58
  • 1
    Or `try { ... } catch (IOException | SQLException ex) { ... }` – Scary Wombat Feb 19 '19 at 05:00
  • see https://stackoverflow.com/questions/3495926/can-i-catch-multiple-java-exceptions-in-the-same-catch-clause – Scary Wombat Feb 19 '19 at 05:02
  • Is it good practice to use generic exception handling ? – ASK Feb 19 '19 at 05:23
  • this https://stackoverflow.com/questions/17635545/try-multi-catch-vs-single-catch might help you – kamlesh pandey Feb 19 '19 at 05:25
  • The proper response to your "error-prone code" is to *fix it*, not to catch exceptions. In the case of both `NullPointerException` and `ArrayIndexOutOfBoundsException`, your code is failing to do basic null and range checking, and the proper course of action is to fix that. – chrylis -cautiouslyoptimistic- Feb 19 '19 at 05:37
  • @Ankush The answer to what your asking depends on the person who you are asking. For me it is a bad practice since it hinders your codes readability. Other developers that might work on your code will find it a hard time to tell what exception you are catching. So if you know what exceptions you are trying to catch, I personally think it is better to explicitly specify them instead of using the generic exception. – Mark just now – Mark Melgo Feb 19 '19 at 06:18

2 Answers2

1

It depends on what will you do with the exception. If you have specific things to do per exception then you can use multiple try catch. If not you can just use the generic exception. Also if you know what kind of exception your code might throw better just use that specific exception.

Mark Melgo
  • 1,477
  • 1
  • 13
  • 30
  • Is it good practice to use generic exception handling ? – ASK Feb 19 '19 at 05:23
  • @Ankush The answer to what your asking depends on the person who you are asking. For me it is a bad practice since it hinders your codes readability. Other developers that might work on your code will find it a hard time to tell what exception you are catching. So if you know what exceptions you are trying to catch, I personally think it is better to explicitly specify them instead of using the generic exception. – Mark Melgo Feb 19 '19 at 06:18
1

You should only catch exceptions that you know how to process properly; examples include IOException where you can do something like retry an operation, return some default value, or rethrow the error; and NumberFormatException, where you're trying to read user input as a number and find that it's garbage, so you can ask the user to try again.

In nearly all cases, you don't actually know what the proper response to "any error" is, and in many cases (most unchecked exceptions, for example), the only thing you'll be doing by catching Exception is masking some underlying problem that needs to be resolved. In general, the only response that is acceptable for a generic unknown exception is "write a log message and abort the current operation" for whatever definition of "current operation" applies (might include things like rolling back a transaction and returning an HTTP 503 status code).

In real-world applications, this last-resort catch Exception is handled by framework code (such as Spring or Jersey) and does these broad cleanup operations. If your code can't do anything better (which generally requires knowing what specifically happened, not just "an exception"), then it should let the exception propagate and use the standard error handlers.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152