3

Throughout the function handleException, the exception encountered is implicit. How does the compiler handle such implicit passing of exceptions? [For e.g. in C++ methods, this pointer is passed to account for the current object. My question is how does a compiler handle exceptions internally - handleException has no arguments.]

 void handleException()
 {
   try {
     throw;
   }
   catch (MyException& e) {
     ...code to handle MyException...
   }
   catch (YourException& e) {
     ...code to handle YourException...
   }
 }

 void f()
 {
   try {
     ...something that might throw...
   }
   catch (...) {
     handleException();
   }
 }
11
  • That's now how it is used in C++. You need to ask on SO how the throw clause works. I have not come across a language that has the concept of implicit exceptions (I suppose they could exist). The better question is why would you want such a concept, it seems very error prone in its usage. Commented Jan 19, 2011 at 6:26
  • @Martin: That is one way it is used in C++, see C++03 §15.1p6.
    – Fred Nurk
    Commented Jan 19, 2011 at 6:32
  • 3
    @Fred Nurk: From n3225. 15.1p8<quote>A throw-expression with no operand rethrows the currently handled exception.</quote>15.1p9<quote>If no exception is presently being handled, executing a throw-expression with no operand calls std::terminate()</quote>. So No. There is NEVER an implicit exception that is thrown. Commented Jan 19, 2011 at 6:36
  • @Martin: Where is the exception mentioned in the throw; statement? If it is not mentioned, is that not implicit? It sounds like you are forming an opinion based on the title alone without actually reading the question body, which explicitly mentions what the OP means by "implicit" (which is also not a term used in the standard).
    – Fred Nurk
    Commented Jan 19, 2011 at 6:43
  • 1
    @Martin York: it's a construct that ideally would be used to translate exceptions when you're using two or more libraries with non-standard exceptions. It never got into general use, I think mostly because earlier (when such libraries abounded) Visual C++ compilers had a bug that caused exception object's destructor to be called twice for this construct. And nowadays libraries just use standard exceptions, so generally no need. Cheers & hth., Commented Jan 19, 2011 at 7:03

1 Answer 1

4

Pay no attention to that man behind the curtain.

Because the compiler knows how it implements exceptions, it can use that internal information. The simplest way is to use a global (or thread local) variable to store info about any "active" exception; then retrieve that, mostly to get type info, and propagate. C++0x partially exposes this information with std::exception_ptr; see §18.8.5 in N3225.

Exceptions are handled differently from parameters; analogy to "this" won't be helpful. The value for "this" is usually treated as a normal pointer parameter, except for being hidden from the user behind the curtain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.