Feb 24, 2014

[C++][Note]function-try-blocks

Rarely known C++ constructs (part 1): Function try blocks
excerpt:

  • Any exceptions caught in constructors or destructors are rethrown implicitly.
  • The currently handled exception is rethrown if control reaches the end of a handler
    of the function-try-block of a constructor or destructor.
    (§ 15.3.15 C++ International Standard/n3337)
  • Return statement in the catch block of a function acts as
    if it was a return statement in the function.
  • The function returns when the control flow reaches
    the end of the catch block.
    If there is no return statement there and
     the function return type is non-void, the behavior is undefined.
  • Function try block of main has some non-intuitive behavior:
    • Exceptions from constructors of objects defined on the namespace scope are not caught.
    • Exceptions from destructors of objects with static duration are not caught.
This totally make sense since main function isn't the _start after all.
Object obj; //Will throw

int main()
try
{

}
catch(...)
{
//Won't catch a thing.
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.