【问题标题】:Why the try-catch block is not able to handle the IllegalAccessException?为什么 try-catch 块无法处理 IllegalAccessException?
【发布时间】:2021-05-26 09:23:45
【问题描述】:

我试图在调用方方法中捕获 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.");
        }
    }
}

【问题讨论】:

  • static void fun() throws IllegalAccessException
  • 它给了你什么错误?

标签: java exception illegalaccessexception


【解决方案1】:

您需要在方法签名中添加throws IllegalAccessException,因为IllegalAccessException 是一个已检查的期望,需要在签名中进行标记。你会使用不需要在签名中添加的 RuntimeExpection。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多