0

认为

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
  }
 }
}

并且想要确定异常的类型,即另一个类的算术异常是否可能?如何

4

1 回答 1

2

您可以创建一个实用程序异常处理程序类,并公开静态方法 exceprionHandler(Exception exception) 方法,您可以在其中处理代码。

像下面

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);
  }
 }
}
于 2018-03-22T10:38:10.213 回答