• 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
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

When to use throws clause???

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand when I need to include the "throws" clause in the method declaration. According to the books I have been reading, I don't need it when it is a Runtime Exception, but I need it for checked exceptions. My problem is that I cannot seem to distinguish between the 2 types and also cannot determine what kind of an exception is being thrown.
For eg : in this code here, which lines will cause an error.
1. class A {
2. void m1() {throw new ArithmeticException();}
3. void m2() {throw new ClassCastException();}
4. void m3() {throw new IllegalArgumentException();}
5. void m4() {throw new IndexOutOfBoundsException();}
6. void m5() {throw new NullPointerException();}
7. void m6() {throw new SecurityException();}
8. }

What is the result of attempting to compile the program?
a. Compiler error at line 2.
b. Compiler error at line 3.
c. Compiler error at line 4.
d. Compiler error at line 5.
e. Compiler error at line 6.
f. Compiler error at line 7.
g. None of the Above
Can anyone explain this?
Thanks
Sharda
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll need to learn to recognize the subclasses of RuntimeException that are in the java.lang package. There aren't that many of them. See the tree diagram at
http://java.sun.com/j2se/1.4/docs/api/java/lang/package-tree.html
Anything that isn't RuntimeException, or a subclass of RuntimeException, is a checked exception that you need to catch or declare in a throws clause.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic