-2
public class Test{  
  public static void main(String args[]){  
  try{  
   int data=25/0;  
   System.out.println(data);  
  }  
  catch( ArithmeticException e)
       {
         System.out.println(e);
       }  
  }  
}  

o/p:java.lang.ArithmeticException: / by zero

In this code even if I catch the exception using the ArithmaticException class or directly the Exception class, the o/p is the same. Then why do we use the subclasses in different catch statements instead of directly using Exception class everytime?

rohegde7
  • 633
  • 9
  • 15
  • There are situations where more than one exception might happen and you want to do different things in each case –  Mar 13 '17 at 18:13
  • Each exception can be handled differently depending on its type. You can use `catch(ExceptionFoo ef){handleFoo(ef)}catch(ExceptionBar eb){handleBar(eb)}`. Using common ancestor will allow you to specify common handling mechanisms for all exceptions of that type (including subtype). – Pshemo Mar 13 '17 at 18:19

3 Answers3

3

You should never use catch(Exception), as that will catch not just the exception you are interested in, but also unintendedly any RuntimeException (because RuntimeException extends Exception) that may have occurred

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • We catch only runtime exception dude. Y would i even care to handle compile time exceptions ? – rohegde7 Mar 13 '17 at 18:21
  • You should only use `catch(Exception)` when you are trying to catch unintended or unknown `RuntimeException`'s and by definition you won't know the type of an unintended or unknown `RuntimeException`. otherwise you should catch the most specific Exception Types that could be thrown. – geneSummons Mar 13 '17 at 18:22
0

You can show messages with more information according to the error and if the error is not within your predictions, you can display a generic message.

In response to question in comments:

try {
    int data = 25 / 0;
    System.out.println(data);
}
catch (ArithmeticException e) {
    System.out.println("Wrong numbers to process");
}
catch (IOException e) {
    System.out.println("Other error message here.");
}
catch (Exception e) {
    /*Here i dont know possible errors in my code, just show a generic message and try resolve the problem for your client.*/ 
     System.out.println("Something wrong./n" + e.getMessage()); 
}
hnefatl
  • 5,860
  • 2
  • 27
  • 49
Rafael Rossi
  • 88
  • 1
  • 8
  • How do u know if the error is out of ur prediction? And how do u handle it ? – rohegde7 Mar 13 '17 at 18:17
  • Something like that. try{ int data=25/0; System.out.println(data); }catch( ArithmeticException e){ System.out.println("Wrong numbers to process"); }catch(IOException e){ System.out.println("Other error message here."); }catch(Exception e){ /*Here i dont know possible errors in my code, just show a generic message and try resolve the problem for your client.*/ System.out.println("Something wrong./n" + e.getMessage()); } – Rafael Rossi Mar 13 '17 at 18:30
  • The final `catch (Exception e)` block is bad practice - as explained by ControlAltDel, any other exceptions that arise should be allowed to propagate upwards as they're unexpected, so you want to know about them. – hnefatl Dec 25 '17 at 18:39
0

Because sometimes you want to catch only ArithmeticException or NumberFormatException. Exception class contains ArithmeticException, NumberFormatException and the other Exception sub classes.

Mustafa Çil
  • 766
  • 8
  • 15