Exception questions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Old Wolf

    Exception questions

    1. What is the difference between #include <stdexcept>
    and #include <exception> ?

    2. Is there a list somewhere of what each standard exception is used
    for? either to throw them, or throw user-defined exceptions
    derived from them? (for example I have been deriving mine from
    std::bad_alloc if there was a memory problem, or std::bad_except ion
    if there was some other problem)

    3. Is it a good idea to make all user-defined exceptions derive from
    std::exception?

    4. While trying to see why my "catch (std::exception &e)" did not catch
    a bad boost::lexical_ cast<>, I discovered that that function throws
    an object derived from "std::bad_cast" , but std::bad_cast is NOT
    derived from std::exception. Are there any other "special case"
    exceptions in the Standard Library that are not derived from
    std::exception?

    5. If you have a handler like:
    catch(std::exce ption &e) { whatever }
    and the exception is thrown like:
    throw foo_exception(" error occurred");
    where foo_exception is derived from std::exception, will it be
    caught? (The exception thrown is a temporary, and you can't bind
    temporaries to non-const references).
  • tom_usenet

    #2
    Re: Exception questions

    On 25 Feb 2004 17:48:54 -0800, oldwolf@inspire .net.nz (Old Wolf)
    wrote:

    I'm no expert on exceptions, but:
    [color=blue]
    >1. What is the difference between #include <stdexcept>
    > and #include <exception> ?[/color]

    exception defines the fundamental exception handling stuff, including
    bad_exception, exception, unexpected, terminate, etc.

    stdexcept defines some standard exception types, like
    std::runtime_er ror, std::logic_erro r, etc.
    [color=blue]
    >
    >2. Is there a list somewhere of what each standard exception is used
    > for? either to throw them, or throw user-defined exceptions
    > derived from them? (for example I have been deriving mine from
    > std::bad_alloc if there was a memory problem, or std::bad_except ion
    > if there was some other problem)[/color]

    std::bad_except ion has a special use - you shouldn't really derive
    from it. If an exception is thrown that breaks exception
    specifications, if the method in question has
    throw(std::bad_ exception) the exception will be turned into a
    std::bad_except ion instead.

    Mostly you should probably derive from one of the exceptions in
    <stdexcept>. Or you could add your own exception base class, deriving
    from std::exception, that adds the facilities you want (perhaps an
    error code, or whatever). This is probably a better idea.
    [color=blue]
    >3. Is it a good idea to make all user-defined exceptions derive from
    > std::exception?[/color]

    It is a reasonable idea, although catch(exception const&) doesn't buy
    you very much compared to catch(...).
    [color=blue]
    >4. While trying to see why my "catch (std::exception &e)" did not catch
    > a bad boost::lexical_ cast<>, I discovered that that function throws
    > an object derived from "std::bad_cast" , but std::bad_cast is NOT
    > derived from std::exception. Are there any other "special case"
    > exceptions in the Standard Library that are not derived from
    > std::exception?[/color]

    std::bad_cast does (or should) derive from std::exception. You must
    have a non-standard compiler in this respect.
    [color=blue]
    >5. If you have a handler like:
    > catch(std::exce ption &e) { whatever }
    > and the exception is thrown like:
    > throw foo_exception(" error occurred");
    > where foo_exception is derived from std::exception, will it be
    > caught? (The exception thrown is a temporary, and you can't bind
    > temporaries to non-const references).[/color]

    Yes, it will be caught - exception catching is handled differently
    from function calling. However, you should catch a std::exception
    const& in any case, since std::exception has no non-const methods
    anyway.

    Tom
    --
    C++ FAQ: http://www.parashift.com/c++-faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

    Comment

    Working...