• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

exception problem:looking at following two code fragments!

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. public class A {
2. private void method1() throws Exception {
3. throw new RuntimeException();
4. }
6. public void method2() {
7. try {
8. method1();
9. } catch(RuntimeException e) {
10. System.out.println("Caught Runtime Exception");
11. } catch(Exception e) {
12. System.out.println("Caught Exception");
13. }
14. }
15. public static void main(String args[]) {
16. A a = new A();
17. a.method2();
18. }
19. }//print "Caught Runtime Exception"
-------------------COMPARE------------------------
public class B {
private void method1() throws Exception {
//throw new RuntimeException();
}
public void method2() {
try {
method1();
} catch(RuntimeException e) {
System.out.println("Caught Runtime Exception");
} catch(Exception e) {
System.out.println("Caught Exception");
}
}
public static void main(String args[]) {
B a = new B();
a.method2();
}
}//compiled,no output?
 
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mellihoney,
declaring an exception in the throws clause does not make it to throw an exception.
We use throws clause to warn any code that might call this method to be beware of any exception.
Sri
 
Mellihoney Michael
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to sri:
just warning?How about the following code:

public class B {
private void method1(){
throw new RuntimeException();
}
public void method2() {
try {
method1();
} catch(RuntimeException e) {
System.out.println("Caught Runtime Exception");
} catch(Exception e) {
System.out.println("Caught Exception");
}
}
public static void main(String args[]) {
B a = new B();
a.method2();
}
}//
[ February 16, 2003: Message edited by: Mellihoney Michael ]
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mellihoney Michael:
to sri:
just warning?How are about the following code:

public class B {
private void method1(){
throw new RuntimeException();
}
public void method2() {
try {
method1();
} catch(RuntimeException e) {
System.out.println("Caught Runtime Exception");
} catch(Exception e) {
System.out.println("Caught Exception");
}
}
public static void main(String args[]) {
B a = new B();
a.method2();
}
}//


RuntimeException is subclass of exception but it is not a checked exception.
I guess you know the difference between a checked exception and a non-checked exception.
A checked exception must declare that it throws the exception or must handle it in a try-catch block.
A non-checked exception, on the other hand,does not need to either declare or throw exceptions.
A good tutorial for exceptions is at this
place
Hope this helps
Sri
 
Mellihoney Michael
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks!
 
reply
    Bookmark Topic Watch Topic
  • New Topic