473,473 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 4194
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.