I am trying to catch an IllegalAccessException in the caller method. But it's not able to catch it. It's giving an error instead.
{
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.");
}
}
}
You need to add throws IllegalAccessException
the the methode signature, because IllegalAccessException is an checked expection and needs to be marked in the signature. Would you use an RuntimeExpection you do not need to added it in the signature.