• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

exception problem

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class ThrowsDemo {
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);
}
}
}
please explain why I am getting this error
C:\JAVA>javac ThrowsDemo.java
ThrowsDemo.java:4: Exception java.lang.IllegalAccessException must be caught, or it must be declared in th
e throws clause of this method.
throw new IllegalAccessException("demo");
^
ThrowsDemo.java:9: Exception java.lang.IllegalAccessException is never thrown in the body of the correspon
ding try statement.
} catch (IllegalAccessException e) {
^
2 errors
Here IllegalAccessException is thrown and caught in Try block.
Please explain this
Thanks in advance
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try changing the method-declaration to:
static void throwMethod() throws IllegalAccessException
This will probably fix both your problems.
/Mike
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a method is throwing an Exception, then the method has to be declare that it *throws* an Exception.
Hence, you would need to modify the code as :

Hope this helps,
Sandeep
SCJP2,OCSD,OCED
 
What are you saying? I thought you said that Santa gave you that. And this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic