throw in function name

This is a discussion on throw in function name within the C++ Programming forums, part of the General Programming Boards category; Hello I've came across some code which looks like: Code: class someclass { public: virtual void somefunction() throw; }; Whats ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    throw in function name

    Hello

    I've came across some code which looks like:

    Code:
    class someclass {
    public:
      virtual void somefunction() throw;
    };
    Whats the meaning of that throw after the function's arguments? When should I use it?

    Thanks for help!

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    It looks like it might be placeholder for the function until the code is written. Sort of a "I need to write this piece of code, and in case I used the function before it's written, the throw will remind me to write it".
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    It's an exception specification that indicates what exceptions a function might throw (in this case, none).

  4. #4
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    I wrote it wrong:

    Its actually:
    Code:
    virtual void somefunction() throw();

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    BTW, it's a good idea to avoid using exception specifications in your own code since they have no real benefit and require all code calling that function to use exception specifications.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by medievalelks View Post
    It's an exception specification that indicates what exceptions a function might throw (in this case, none).
    Thanks for the correction.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    BTW, it's a good idea to avoid using exception specifications in your own code since they have no real benefit and require all code calling that function to use exception specifications.
    Are you saying that exception scecifications are a virus in the sense const and unsigned/signed are (because it doesn't seem that way)?

    As I understand it, if an exception is thrown nevertheless it is not a compile time error, but at runtime instead of propagating the exception, unexpected() is called which calls an unexpected handler which is not allowed to return but has to terminate the program one way or another. This way the promise made in the exception specification is enforced at runtime.

    If I'm not mistaken they are useless in case of template code, because you can't take any responsibility for what any particular template argument type might throw.

    You may try different combinations of commenting/uncommenting the two throw's in foo.

    Code:
    #include <exception>
    
    void foo() throw()
    {
        throw 1;
    }
    
    void bar()
    {
        foo();
    }
    
    void oh_my_god() throw()
    {
        std::cout << "Program was terminated by unexpected exception\n";
        exit(1);
    }
    
    int main()
    {
        std::set_unexpected(oh_my_god);
        try {
            bar();
            std::cout << "Everything's fine\n";
        }
        catch (...) {
            std::cout << "Exception happened\n";
        }
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    But the compiler doesn't complain if your function can throw an exception that isn't specified, and the only option in that case is to terminate your program, which is a pretty bad thing.
    Take this example:
    Code:
    void OldFunc()  // NOTE:  no throws declaration, so it might throw anything.
    {
       ...
    }
    
    void NewFunc() throw std::exception
    {
       OldFunc();
       ...
    }
    Now if OldFunc() only throws std::exception then everything is fine, but the minute someone makes a change to OldFunc() and throws something else, your program will now terminate if that exception happens, even if you have a catch block further up the call chain that does handle that new exception.

    Boost says exception specifications are bad, as do the authors of many C++ books.

    In a language like Java where exception specifications were built into the language right from the beginning, they do make sense, but since they are optional in C++ and the compiler is unable to detect cases where an unspecified exception may be thrown, it's just another one of those good ideas that didn't quite work in C++.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by cpjust View Post
    Now if OldFunc() only throws std::exception then everything is fine, but the minute someone makes a change to OldFunc() and throws something else, your program will now terminate if that exception happens, even if you have a catch block further up the call chain that does handle that new exception.
    So change the specification and recompile, right? Then the code that uses the old specification will give a compile error, right?

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The idea was probably to provide "comments" whose correctness is enforced at run-time. But comments tend to go out of sync with the code, and it is often that the code is correct and the comment is wrong, so this mechanism is quite inconvenient indeed. (But to write robust code you often need to know what type of exceptions you can expect.)

    But anyway, your example is now the other way round. It's not that if a function has a throw specification, functions calling that (functions up the chain) have to have one too. Instead in your example, if some function down the chain doesn't make any promises, functions calling that one can't make any promises either.

    There are, however, certain operations that should never throw (e.g the exception class itself, swap function), so a no-throw specification might be in order.

    Edit: well, if the exception specification of something down the chain changes, every function up the chain that calls this function and uses exception specification needs to be changed. It might be an enormous work to track down, change and recompile all of them.
    On the other hand, if a low-level function is modified to throw new types of exceptions, lots of user code would have to be changed anyway.
    Last edited by anon; 07-28-2008 at 11:45 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by anon View Post
    There are, however, certain operations that should never throw (e.g the exception class itself, swap function), so a no-throw specification might be in order.
    Destructors and delete operators must never throw also.

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, 02:01 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  3. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 11:08 AM
  4. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 07:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21