473,217 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,217 software developers and data experts.

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 4178
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: T.T.H. | last post by:
Hi I am coding a C++ COM object which I want to access in Python. For that I do have some detail questions and need help since I am not that familiar with the documentation itself of Python and...
28
by: dcrespo | last post by:
Hi all, How can I get a raised exception from other thread that is in an imported module? For example: --------------- programA.py ---------------
9
by: ljlevend | last post by:
I have two questions related to FileStreams. 1. Is there any way to determine whether a file has the permissions that are required by a FileStream constructor? For example, given the following...
4
by: Steve | last post by:
I have read a couple articles online, read my Jesse Liberty book but I am still confused as to just what the best practices are for using exceptions. I keep changing how I'm working with them and...
3
by: JohnDeHope3 | last post by:
First let me say that I understand that Asp.Net wraps my exception in an HttpUnhandledException. I have found a lot of discussion about that on the web, which was informative, but not helpful. Let...
16
by: marc_r_bertrand | last post by:
To all asp/db pros: The quiz code below works. But there is a problem when too many questions are answered (radio buttons clicked). I am not an asp pro. So, is there a pro out there or an...
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
14
by: Rex | last post by:
Re: Looking for Tips/Writeup on overall approach to Exception Processing Hi All - I am fairly new to C# and am wondering how to best implement (overall) Exception Processing within my...
4
by: Miro | last post by:
Vb.2005 express I am having an issue where when in vb.2005 express and running it internally, I can catch the exception, but when I publish the project I get a crash - and a JIT error. This...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.