Monday, October 6, 2008

c++: function try-catch block

There is no hidden features in c++. Everything you can find in specification.
Somebody from Somewhere
There is an interesting feature in c++ - a function try-catch block. You can replace function body with try-catch block.
void function()
try
{
        //do smth
}
catch(...)
{
        //handle exception
}
This is almost similar to
void function()
{
        try
        {
                //do smth
        }
        catch(...)
        {
                //handle exception
        }
}
Quick notes:
  • the scope and lifetime of the parameters of a function or constructor extend into the function try-catch blocks
  • A function try-catch block on main() does not catch exceptions thrown in destructors of objects with static storage duration. In code below catch won't be called
    class A
    {
            public:
                    ~A()
                    {
                            throw "Exception in ~A";
                    }
    };
    
    int main(int argc, char **argv)
    try
    {
            static A a;
    }
    catch(const char *e)
    {
            std::cout << e << std::endl;
    }
    
  • A function try-catch block on main() does not catch exceptions thrown in constructors/destructors of namespace/global namespace scope objects. In code below catch won't be called
    namespace N
    {
            class A
            {
                    public:
                            A()
                            {
                                    throw "Exception in A";
                            }
            };
    
            A a;
    }
    
    int main(int argc, char **argv)
    try
    {
    }
    catch(const char *e)
    {
            std::cout << e << std::endl;
    }
  • The run time will rethrow an exception at the end of a function try-catch block's handler of a constructor or destructor. All other functions will return once they have reached the end of their function try block's handler
    class A
    {
            public:
                    A()
                    try
                    {
                            throw "Exception in A";
                    }
                    catch(const char *e)
                    {
                            std::cout << "In A: " << e << std::endl;
                    }
    };
    
    int main(int argc, char **argv)
    try
    {
            A a;
    }
    catch(const char *e)
    {
            std::cout << "In main: " << e << std::endl;
    }
    
    The output
    In A: Exception in A
    In main: Exception in A
    int function()
    try
    {
            throw "Exception in function";
    }
    catch(const char *e)
    {
            std::cout << "In function: " << e << std::endl;
    
            return 1;
    }
    
    int main(int argc, char **argv)
    try
    {
            std::cout << function() << std::endl;
    }
    catch(const char *e)
    {
            std::cout << "In main: " << e << std::endl;
    }
    
    The output
    In function: Exception in function
    1
    

2 comments:

Tsu said...

Thanks for this post.. But is a function try-block part of standard c++?
krlinus (at) yahoo (com)

Ni@m said...

Hello, Tsu!
Yes, this is a part of standard: chapter 15, function-try-block.