Thread: function throw

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    function throw

    Hello

    What does that mean:

    Code:
    void some_function(int blabla) try {
    }
    catch (...) {
    }
    And:

    Code:
    void some_function(int blabla) throw {
    }
    When is it smart to define functions like that?

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    The first one catches exceptions thrown anywhere in the function. It's a special syntax designed for the intialization list in constructors. The second one I'm not sure about. I don't think it's legal.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The first is a function level try block. It's probably good to catch exceptions thrown in this function, to do some clearing-up (before possibly rethrowing the error).

    The second should be invalid, but the aim is to specify the types of exceptions this function might throw. It is important, that the function (or an object used it) wouldn't throw any unspecified exceptions (this is the reason why it is not used much in the C++ libraries, because with templates you'll never know what exceptions one or another object might throw.

    Code:
    void some_function() throw(std::exception); //function promises to throw only exceptions of type std::exception
    void another_function() throw(); // promise to throw no exceptions

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The motivation for the function-level try block comes from constructors. Consider a constructor that wants to catch all exceptions. A naive approach might look like this:
    Code:
    class A : public B
    {
      C c;
      D d;
    
    public:
      A();
    };
    
    A::A()
    {
      try {
        // do initialization
      } catch(...) {
        // handle error
      }
    }
    As I said, it's naive. What initialization is there to do? It has all been done already, in the initializer list. If I had it written explicitly:
    Code:
    A::A()
      : B(), c(), d()
    { try {} catch(...) {} }
    Exceptions from the B, C and D constructors are outside the try. They're not caught.

    The function-level try block looks like this:
    Code:
    A::A() try
      B(), c(), d()
    {
    } catch(...) {}
    Now the initializer list is inside the try and its exceptions can be caught.

    Making function-level try blocks available for normal functions was a matter of consistency.

    Note, however, that there still is one difference: the catch block of a constructor must exit with an exception; if it doesn't throw one, the caught one is automatically re-thrown.



    As for exception specifications, there is a simple and nearly universally applicable rule for their use: don't.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. throw in function name
    By l2u in forum C++ Programming
    Replies: 10
    Last Post: 07-28-2008, 12:37 PM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 10:08 AM
  5. Replies: 28
    Last Post: 07-16-2006, 11:35 PM