有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在catch子句中使用异常实例

public class ExceptionObject {

    public static void main(String[] args)
    {
        Exception exceptionObj = new Exception();
        int a=1, b=0; 

        try
        {
            int c=a/b;
        }
        catch(exceptionObj)
        {
            exceptionObj.printStackTrace();
        }
    }

}

为什么我不能使用“exceptionObj”,它是catch子句中类Exception的一个实例。 请告知,会有帮助的


共 (1) 个答案

  1. # 1 楼答案

    您可以尝试以下方法:

    int a = 1, b = 0;
    
    try {
     int c = a / b;
    } catch (Exception exceptionObj) {
     exceptionObj.printStackTrace();
    }