1

Why do we use the more specific exceptions like IndexOutOfRangeException and DivideByZero exceptions when we can catch them in a catch block like this:

try
{
    //Some Work
}
catch(Exception E){}
Chris
  • 27,210
  • 6
  • 71
  • 92
DarkThunder
  • 33
  • 1
  • 8

1 Answers1

3

You should only ever write specific code to handle exceptions that you reasonably expect to be thrown. If you understand that specific code could throw a specific type of exception then you can determine exactly what to do in that situation. If you catch absolutely any type of exception then you will have no idea what the cause was and thus you can't know what should be done about it.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46