为什么 try-catch 块无法处理 IllegalAccessException?
Why the try-catch block is not able to handle the 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 是一个checked exception,需要在签名中标明。您会使用 RuntimeExpection 吗?您不需要将其添加到签名中。
我试图在调用方方法中捕获 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 是一个checked exception,需要在签名中标明。您会使用 RuntimeExpection 吗?您不需要将其添加到签名中。