This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception doubt" Watch "Exception doubt" New topic
Author

Exception doubt

Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
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

Joined: Dec 12, 2005
Posts: 65
just declare it to be thrown


static void throwMethod() throws IllegalAccessException


SCJP(1.4), SCWCD(1.4), SCBCD(1.3), SCDJWS
Raghusham Sankargal
Ranch Hand

Joined: Oct 10, 2005
Posts: 59
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.


Raghusham Sankargal
 
Consider Paul's rocket mass heater.
 
subject: Exception doubt
 
Similar Threads
exception problem
throw an Exception....
JavaCaps Mock #2 Q60 on exceptions
Exception Problem
Exception