2

Take this piece of code:

try
         {
             FileReader f=new FileReader("D:/sda.t");            
         }catch(FileNotFoundException e)
         {
             e.printStackTrace();
         }catch(Exception f)
         {
             f.printStackTrace();
         }
  1. Why do we follow this approach?
  2. Why don't we just use Exception objection to catch all exceptions rather going for specific ones?
jmj
  • 237,923
  • 42
  • 401
  • 438
Abhilash28
  • 645
  • 1
  • 9
  • 15
  • 1
    Because you want to handle each exception in a different manner, rather than handle them all the same? – Daniel Sep 11 '14 at 06:36
  • possible duplicate of [Why is the Catch(Exception) almost always a bad Idea?](http://stackoverflow.com/questions/2416316/why-is-the-catchexception-almost-always-a-bad-idea) – Daniel Sep 11 '14 at 06:37
  • Duplicate [Why does order matter when catching exceptions?](http://stackoverflow.com/q/15167899/1066779) – Abhishek Nayak Sep 11 '14 at 06:47

6 Answers6

2

As others said, it is a bad idea to catch all Exceptions as usually you want to handle them differently.

Though you need to also be aware that catching Exception also catches all RuntimExceptions, which you do generally not want to catch. These include things like a NullPointerException.

If you do not catch RuntimeExceptions then the program will just die once it reaches one and you will be notified of it, if you catch Exception and forget to handle anything that shows the exception, then you will miss RuntimeExceptions, which denote programmer errors.

skiwi
  • 66,971
  • 31
  • 131
  • 216
1

Why do we follow this approach? Why don't we just use Exception objection to catch all exceptions rather going for specific ones?

Simple, you do this when you want to do specific tasks when a particular exception occurred.

lxcky
  • 1,668
  • 2
  • 13
  • 26
1

Because in case of a specific exception that you are actually expecting to happen, you are going to handle it in a different way. For example, you may want to create the file when you get FileNotFoundException.

If you go broad to narrow as in the example you provided, then FileNotFoundException will never be caught, because a broader one, Exception is used first. So broad to narrow is useless.

mostruash
  • 4,169
  • 1
  • 23
  • 40
1

3 reasons:

  1. To separate error handling code.
  2. To be able to propagate error reporting up the call stack of methods.
  3. To group and deferentiate error types.

Check out the docs and also this answer.

Community
  • 1
  • 1
aviad
  • 8,229
  • 9
  • 50
  • 98
0

If the only thing you do with the exception is printStackTrace(), then there is no/little point. The point is different handling code can be associated with different exceptions, and matching a thrown exception against a catch list is done in the order as specified (with a first-match only selection mechanism).

Erwin Smout
  • 18,113
  • 4
  • 33
  • 52
0

in java7 or newer you can use multicatch statement:

  catch(SQLException | IOException e) {
    logger.log(e);
}
smajlo
  • 972
  • 10
  • 21