This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Doug Slattery
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20993
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Doug Slattery
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20993
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic