• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception doubt

 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting error as below why ....... what is the solution ......
---------- Javac ----------
primk.java:6: unreported exception java.lang.IllegalAccessException; must be caught or declared to be thrown
throw new IllegalAccessException("demo");
^
primk.java:14: exception java.lang.IllegalAccessException is never thrown in body of corresponding try statement
catch (IllegalAccessException e)
^
2 errors
-----------------------------------
public class primk
{
static void throwMethod()
{
System.out.println("Inside throwMethod.");
throw new IllegalAccessException("demo");
}
public static void main(String args[])
{
try
{
throwMethod();
}
catch (IllegalAccessException e)
{
System.out.println("Caught " + e);
}
}
}
 
Gyan Shankar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just declare it to be thrown


static void throwMethod() throws IllegalAccessException
 
Raghusham Sankargal
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

[LIST]IllegalAccessException is a checked exception. That is why if it thrown or occurs, then either it should be caught or should be escalate (main class cannot escalate as it the highest level function). Either you add a respected catch or add a throws declaration. If you add catch then the error thrwon in main won't go (I think). So to remove the errors add the throws declaration as already said in the message on top of my message. Hope this your issue is resolved.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic