Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Exception doubt
 
Similar Threads
Exception Problem
throw an Exception....
Exception
exception problem
JavaCaps Mock #2 Q60 on exceptions