0

Suppose

class Example1
{
 public static void main(String args[])
 {
  try{
     int num1=30, num2=0;
     int output=num1/num2;
     System.out.println ("Result: "+output);
  }
  catch(Exception e){
     //and in want to determine the type of exception ie- Arithmetic 
      exception on the another class IS IT POSSIBLE? AND HOW
  }
 }
}

and in want to determine the type of exception ie- Arithmetic exception on the another class IS IT POSSIBLE? AND HOW

Anindyo Bose
  • 153
  • 2
  • 11
  • dou you want to catch exception in Example1 and throw it on say Example2 Class ? – Afgan Mar 22 '18 at 10:04
  • Yes That is what i want to do. And print its type. Say Arithmetic exception @Afgan – Anindyo Bose Mar 22 '18 at 10:12
  • its not seems to be valid cases from exception handling point of view, exception is propagated in reverse order. Why other class or module need to know about your exception if they are not calling exception prone method ? can you please explain a valid use case – Afgan Mar 22 '18 at 10:17
  • I want to define a class where all the exceptions is to be handled. so from multiple class if exception occurs then I can use that class to determine its type – Anindyo Bose Mar 22 '18 at 10:22
  • You can create a utility exception handler class, and expose static method exceprionHandler(Exception exception) method, where you can do handling code. – Afgan Mar 22 '18 at 10:29
  • Can You please help me with an example? – Anindyo Bose Mar 22 '18 at 10:34

1 Answers1

2

You can create a utility exception handler class, and expose static method exceprionHandler(Exception exception) method, where you can do handling code.

like below

public class ExceptionHandler {

public static  void exceptionHandler(Exception exception){
  //Handling code
}
}

class Example1
{
 public static void main(String args[])
 {
  try{
     int num1=30, num2=0;
     int output=num1/num2;
     System.out.println ("Result: "+output);
  }
  catch(Exception e){
    ExceptionHandler.exceptionHandler(e);
  }
 }
}
Afgan
  • 1,022
  • 10
  • 32