为什么 try-catch 块无法处理 IllegalAccessException?

我试图在调用方方法中捕获 IllegalaccessException。但它无法抓住它。相反,它给出了一个错误。

{
    static void fun()
    {
      try{
        System.out.println("Inside fun(). ");
        throw new IllegalaccessException("demo");
      }catch(IllegalaccessException e){
            throw e;
      }
    }
    public static void main(String args[])
    {
        try
        {
            fun();
        }
        catch(IllegalaccessException e)
        {
            System.out.println("caught in main.");
        }
    }
}

jl316 回答:为什么 try-catch 块无法处理 IllegalAccessException?

需要在methode签名中加上throws IllegalAccessException,因为IllegalAccessException是一个checked期望,需要在签名中注明。您是否会使用不需要在签名中添加的 RuntimeExpection。

本文链接:https://www.f2er.com/358982.html

大家都在问