Exception questions

O

Old Wolf

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).
 
T

tom_usenet

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top