This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception decalaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception decalaration" Watch "Exception decalaration" New topic
Author

Exception decalaration

Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
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

Joined: Mar 01, 2005
Posts: 485
yes
since we are not handle the exception by try block we need to explictly declare the throws in the method bad method.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Exception decalaration
 
Similar Threads
"Handle or Declare" Rule for Checked Exceptions
Exception declaration
throw exception question
Exceptions doubt
Exception