• 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

try/catch question

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a java code, it has following:
public void method_A() {
try {
...
...
}catch(Exception_1 e ) {
...
}catch(Exception_2 e) {
...
}
}
Now if I have another method called "method_B()" in the same class:

public void method_B() {
method_A();
}
this method_B only calls method_A. Then how should I handle the exception in this method, which one(s) of the following is(are) correct ?
1. public void method_B() throws Exception_1, Exception_2 {
method_A();
}
2. public void method_B() {
try {
method_A();
}catch(Exception_1 e ) {
...
}catch(Exception_2 e) {
...
}
}
3. public void method_B() {
mthod_A();
}
Thanks,
Ian
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ian
If both of the exceptions that A can throw are caught and handled in A then you dont need to do anything special in B to account for them.
The compiler will look at A to see if it throws any exceptions that B might need to cach if so then it tells you that there is an uncaught exception, in your case if A can only throw the two checked exceptions then the compiler will not complain becasue you ahve them both caught in A (unless A then rethrows the same or a different exception).
You last method would the correct one.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Just food for more thought...

Pretty self-explanatory, but are there any questions?
Good Luck,
-Dirk Schreckmann
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic