469,620 Members | 1,857 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,620 developers. It's quick & easy.

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_exception
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::exception &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).
Jul 22 '05 #1
1 4008
On 25 Feb 2004 17:48:54 -0800, ol*****@inspire.net.nz (Old Wolf)
wrote:

I'm no expert on exceptions, but:
1. What is the difference between #include <stdexcept>
and #include <exception> ?
exception defines the fundamental exception handling stuff, including
bad_exception, exception, unexpected, terminate, etc.

stdexcept defines some standard exception types, like
std::runtime_error, std::logic_error, etc.

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_exception
if there was some other problem)
std::bad_exception 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_exception 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.
3. Is it a good idea to make all user-defined exceptions derive from
std::exception?
It is a reasonable idea, although catch(exception const&) doesn't buy
you very much compared to catch(...).
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?
std::bad_cast does (or should) derive from std::exception. You must
have a non-standard compiler in this respect.
5. If you have a handler like:
catch(std::exception &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).


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
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

28 posts views Thread by dcrespo | last post: by
9 posts views Thread by ljlevend | last post: by
3 posts views Thread by JohnDeHope3 | last post: by
132 posts views Thread by Zorro | last post: by
4 posts views Thread by Miro | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.