假设
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
}
}
}
并且想要确定异常的类型,即算术 另一个类的异常可以吗?以及如何实现
最佳答案
您可以创建一个实用程序异常处理程序类,并公开静态方法 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);
}
}
}
关于java - 在java中如何捕获类中的异常并将其抛出到另一个类以获取/打印异常的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49425392/