Throwing unexpected exceptions

Discussion in 'C++' started by Urs Thuermann, Jun 16, 2011.

  1. In Stroustrup's "The C++ programming language" I read that throwing an
    exception that is not declared to be expected in a function causes
    std::unexpected() to be called. I tried the difference with the following code

    #include <iostream>

    #ifdef NO_EXCPT
    int foo() throw()
    #else
    int foo() throw(int)
    #endif
    {
    throw 1;
    }

    int main()
    {
    try {
    foo();
    } catch (...) {
    std::cerr << "Exception caught\n";
    }
    }

    But my question is, is there any good reason to intentionally throw
    unexpected exceptions? If not, shouldn't the compiler warn about code
    that clearly does so, i.e. the above code with NO_EXCPT defined?

    urs
     
    Urs Thuermann, Jun 16, 2011
    #1
    1. Advertising

  2. Urs Thuermann

    Rune Allnor Guest

    On Jun 16, 10:50 am, Urs Thuermann <> wrote:
    > In Stroustrup's "The C++ programming language" I read that throwing an
    > exception that is not declared to be expected in a function causes
    > std::unexpected() to be called.  I tried the difference with the following code
    >
    >         #include <iostream>
    >
    >         #ifdef NO_EXCPT
    >         int foo() throw()
    >         #else
    >         int foo() throw(int)
    >         #endif
    >         {
    >                 throw 1;
    >         }
    >
    >         int main()
    >         {
    >                 try {
    >                         foo();
    >                 } catch (...) {
    >                         std::cerr << "Exception caught\n";
    >                 }
    >         }
    >
    > But my question is, is there any good reason to intentionally throw
    > unexpected exceptions?


    How would you, the programmer, know what exceptions
    your function might throw? How do you define 'throw'?
    Consider the function

    void f()
    {
    std::vector<double> v;
    v.reserve(1e80); // Not enough memory
    }

    Clearly, few systems have enough memory available to
    accomodate the requested amount of memory, so the
    call v.reserve() will throw a bad_alloc exception.

    But which class / function 'threw'? Your function?
    std::vector<T>::reserve() ?

    > If not, shouldn't the compiler warn about code
    > that clearly does so, i.e. the above code with NO_EXCPT defined?


    How would the compiler be able to analyze all the
    internal code to deduce which exceptions might and
    which might not be thrown? One might easily imagine
    a situation where your function calls a pre-compiled
    library, that might throw. How would the compiler
    be able to analyze that kind of situation?

    Not at all obvious.

    Rune
     
    Rune Allnor, Jun 16, 2011
    #2
    1. Advertising

  3. Urs Thuermann

    ThosRTanner Guest

    On Jun 16, 11:13 am, Rune Allnor <> wrote:
    > On Jun 16, 10:50 am, Urs Thuermann <> wrote:
    >
    >
    >


    > > But my question is, is there any good reason to intentionally throw
    > > unexpected exceptions?

    >
    > How would you, the programmer, know what exceptions
    > your function might throw? How do you define 'throw'?
    > Consider the function
    >
    > void f()
    > {
    >    std::vector<double> v;
    >    v.reserve(1e80);        // Not enough memory
    >
    > }
    >
    > Clearly, few systems have enough memory available to
    > accomodate the requested amount of memory, so the
    > call v.reserve() will throw a bad_alloc exception.
    >
    > But which class / function 'threw'? Your function?
    > std::vector<T>::reserve() ?
    >
    > > If not, shouldn't the compiler warn about code
    > > that clearly does so, i.e. the above code with NO_EXCPT defined?

    >
    > How would the compiler be able to analyze all the
    > internal code to deduce which exceptions might and
    > which might not be thrown? One might easily imagine
    > a situation where your function calls a pre-compiled
    > library, that might throw. How would the compiler
    > be able to analyze that kind of situation?


    It's trivial for the compiler to tell that a function might throw, and
    warn, because the signature of each function tells you whether it
    might throw.

    pc-lint does this, and very useful it is if you don't want your
    program to terminate because someone threw an exception they weren't
    allowed to

    It doesn't need to examine the code, in the same way it doesn't need
    to examine the possible code paths to determine to determine that
    this:

    void func(char * s);

    myfunc(char const *s) { func(s) };

    is invalid, even if func never updates s. Yet no one complains about
    this behaviour.
     
    ThosRTanner, Jun 16, 2011
    #3
  4. Rune Allnor <> writes:

    > How would the compiler be able to analyze all the
    > internal code to deduce which exceptions might and
    > which might not be thrown? One might easily imagine
    > a situation where your function calls a pre-compiled
    > library, that might throw. How would the compiler
    > be able to analyze that kind of situation?
    >
    > Not at all obvious.


    If I write

    void foo() throw() { throw 0; }

    the compiler can easily see that the code can actually throw an
    exception not listed in the declaration and could warn about it.

    In the following example

    void bar();
    void foo() throw() { bar(); }

    the compiler should IMHO assume that bar() might throw any exception,
    i.e. foo() might throw any exception, and should warn since the
    declaration foo() is probably wrong.

    OTOH, if I write

    void bar() throw();
    void foo() throw() { bar(); }

    I wouldn't expect a warning from the compiler (when compiling foo())
    since everything seems correct, even if bar() is defined (somewhere
    else) to throw some exception. Compiling bar() should then probably
    produce a warning.

    urs
     
    Urs Thuermann, Jun 16, 2011
    #4
  5. Urs Thuermann

    Balog Pal Guest

    "Urs Thuermann" <>
    > In the following example
    >
    > void bar();
    > void foo() throw() { bar(); }
    >
    > the compiler should IMHO assume that bar() might throw any exception,
    > i.e. foo() might throw any exception, and should warn since the
    > declaration foo() is probably wrong.


    In wide practice that would be a warning immediately making Alf's
    sillywarning list. Only good as noise.
     
    Balog Pal, Jun 16, 2011
    #5
  6. Urs Thuermann

    Dombo Guest

    Op 16-Jun-11 10:50, Urs Thuermann schreef:
    > In Stroustrup's "The C++ programming language" I read that throwing an
    > exception that is not declared to be expected in a function causes
    > std::unexpected() to be called. I tried the difference with the following code
    >
    > #include<iostream>
    >
    > #ifdef NO_EXCPT
    > int foo() throw()
    > #else
    > int foo() throw(int)
    > #endif
    > {
    > throw 1;
    > }
    >
    > int main()
    > {
    > try {
    > foo();
    > } catch (...) {
    > std::cerr<< "Exception caught\n";
    > }
    > }
    >
    > But my question is, is there any good reason to intentionally throw
    > unexpected exceptions? If not, shouldn't the compiler warn about code
    > that clearly does so, i.e. the above code with NO_EXCPT defined?



    If you consider using exception specifications in your code I recommend
    you read this article first: http://www.gotw.ca/publications/mill22.htm
     
    Dombo, Jun 16, 2011
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. C

    Throwing Exceptions

    C, Nov 6, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    374
    Kevin Spencer
    Nov 6, 2003
  2. =?Utf-8?B?Q2xpdmU=?=

    Throwing Exceptions

    =?Utf-8?B?Q2xpdmU=?=, Feb 20, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    342
    S. Justin Gengo
    Feb 20, 2004
  3. Loic Minier
    Replies:
    1
    Views:
    2,080
    Loïc Minier
    Aug 19, 2003
  4. John Leonard
    Replies:
    7
    Views:
    347
  5. Rainer Grimm

    Aw: Throwing unexpected exceptions

    Rainer Grimm, Jun 18, 2011, in forum: C++
    Replies:
    1
    Views:
    251
    ThosRTanner
    Jun 21, 2011
Loading...

Share This Page