wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is there a purpose for try/catch on a checked exception? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is there a purpose for try/catch on a checked exception?" Watch "Is there a purpose for try/catch on a checked exception?" New topic
Author

Is there a purpose for try/catch on a checked exception?

Doug Slattery
Ranch Hand

Joined: Sep 15, 2007
Posts: 294
Howdy folks,

Here's one probably for the ranchhands and above. My mind is turning to oatmeal now after several hours of review. This "vision" popped into my head re-reading the two minute warning in ch. 5.

Since checked exceptions get caught (meaning detected and not caught via catch { } ) at compile time, doesn't that imply that the try/catch block will never get executed when an exception is encountered because the compile fails and there's no executable to run? Or, will the compiler let it slide if the thrown exception is handled and just mosey along. Is the compiler pseudo-executing code chunks to check for exceptions? Is it just part of the semantics of the language that requires try/catch even though the compiler is saying "hey programmer dude, get a grip!" .

Aloha,
Doug

-- Nothing is impossible if I'mPossible
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

Since checked exceptions get caught (meaning detected and not caught via catch { } ) at compile time, doesn't that imply that the try/catch block will never get executed when an exception is encountered because the compile fails and there's no executable to run?


The compiler checks for compilation errors. It doesn't run the application at compile time to make sure that runtime errors won't occur.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Doug Slattery
Ranch Hand

Joined: Sep 15, 2007
Posts: 294
Thanks Henry,

The compiler checks for compilation errors. It doesn't run the application at compile time to make sure that runtime errors won't occur.


That still leads me back to my point. What happens when the compiler encounters the statement:



Is the throw statement interpreted by the compiler? If so, what's the use for the try/catch block if compilation fails because of this? Is the compiler walking the Exception tree to see that MyCheckedException is a subclass? If so, that still leads back to the same question - what's the use of a try/catch block?

Aloha,
Doug

-- Nothing is impossible if I'mPossible
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

Is the throw statement interpreted by the compiler?


Well, no... The compiler will do some checking to make sure that it is a valid object that may be thrown. If it is a checked exception, it will check to make sure that it will be caught, or declare as thrown.

And once this is done, the compiler will generate code that will perform the throw. It doesn't actually run any application code during compile time.

Henry
 
Don't get me started about those stupid light bulbs.
 
subject: Is there a purpose for try/catch on a checked exception?