Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception decalaration

 
Kedar Dravid
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the book Java2:Sun Certified Programmer and Developer

The following compiles just fine:
class TestEx {
public static void main (String [] args) {
badMethod();
}
static void badMethod() { // No need to declare an Error
doStuff();
}
static void doStuff() { //No need to declare an Error
try {
throw new Error();
}
catch(Error me) {
throw me; // We catch it, but then rethrow it
}
}
}
If we were throwing a checked exception rather than Error, then the
doStuff()method would need to declare the exception.

My question:
It is stated that the doStuff()method would need to declare the exception if we were throwing a checked exception rather than Error.
My question:
Would the method badMethod also need to declare the exception if we were throwing a checked exception rather than Error ?
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes
since we are not handle the exception by try block we need to explictly declare the throws in the method bad method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic