java - 为什么 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.");
}
}
}
解决方案
您需要添加throws IllegalAccessException
方法签名,因为 IllegalAccessException 是已检查的预期,需要在签名中进行标记。您是否会使用不需要在签名中添加的 RuntimeExpection。
推荐阅读
- firebase - 使用存储在数据中的数据库安全规则中的字段
- c++ - 最小交换次数 2 - 对向量进行升序排序所需的最小交换次数
- ejabberd - 如何将我的自定义身份验证脚本连接到 ejabberd?
- string - 从函数内调用python 3中的函数
- python - Django/Python - 上传自定义脚本 - 如何运行它们?
- tensorflow - 我一直在使用 Keras 训练我的图像分类模型。在我将类别数量从 3 个增加到 4 个之后,我得到了一个错误?
- java - 模拟按键?
- github - 使用 NuGet 发布和使用 GitHub 包存储库:无法加载服务索引错误
- google-cloud-platform - 删除 BigQuery 中数组之间的重复值
- google-cloud-platform - 如何在其区域之外私下共享 Google Cloud SQL