473,385 Members | 1,642 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,385 software developers and data experts.

Nested exceptions (ala Java)

Consider the following operation:

class C
{
public:
...
virtual void override_me() = 0;

struct AnException : public std::runtime_error {
AnException(const std::string& arg)
: std::runtime_error(arg)
{}
};

void run_some_operation()
{
try {
...
this->override_me();
...
} catch(const std::exception& e) {
throw AnException(e.what()
+ " raised from 'override_me()' "
+ " in 'run_some_operation()'");
}
}

void run_another_operation()
{
try {
...
this->override_me();
...
} catch(const std::exception& e) {
throw AnException(e.what()
+ " raised from 'override_me()' "
+ " in 'run_another_operation()'");
}
}

...
}; // class C
Now, I was doing some Java a few months ago, and I encountered Java's way of
doing this. Each 'Throwable' has another member, a 'Cause'. So above, the
'Cause' of the 'AnException' would be whatever exception 'override_me()' threw.

In Java, of course, you can get a stack trace to the point where the exception
was thrown, so storing a nested exception allows the nested exception's
stacktrae to also be shown. In the above example, the stacktrace for the
'AnException' would be where it is thrown in the 'run_some_operation()'
function, while the stack trace for the exception that triggered that
exception be from wherever it was thrown. (I hope that's not too confusing.)

Now, in C++ exceptions store stack traces up to the point they were thrown
from (it would be nice if they could...) so nesting exceptions like this isn't
too useful. But it still allows a little bit more detail to be stored, and
avoids having to do some text processing (appending the message of one
exception as well as its name to the message of the other one).

I'm thinking of creating a new base class that all my exceptions derive from
that allows this behaviour. Do you guys think it'd be a useful feature to
immitate? Is there anything I'm not thinking of? (I know I'd need to store a
copy of the exception thrown, which I can't do for standard exceptions. But
my own exceptions will all have a 'virtual copy constructor' that I'll use to
get a duplicate of them to store, so that'll be no problem.)

Thanks, please be gentle with the flames if I'm talking cobblers :-)

--
To reply, take of all ZIGs !!
Feb 20 '06 #1
4 1976
TB
Asfand Yar Qazi skrev:
Consider the following operation:

class C
{
public:
...
virtual void override_me() = 0;

struct AnException : public std::runtime_error {
AnException(const std::string& arg)
: std::runtime_error(arg)
{}
};

void run_some_operation()
{
try {
...
this->override_me();
...
} catch(const std::exception& e) {
throw AnException(e.what()
+ " raised from 'override_me()' "
+ " in 'run_some_operation()'");
}
}


C++ is not Java. The statement

e.what() + "..." + "...";

does not do what you think it does.

--
TB @ SWEDEN
Feb 20 '06 #2
TB wrote:
Asfand Yar Qazi skrev:
Consider the following operation:

class C
{
public:
...
virtual void override_me() = 0;

struct AnException : public std::runtime_error {
AnException(const std::string& arg)
: std::runtime_error(arg)
{}
};

void run_some_operation()
{
try {
...
this->override_me();
...
} catch(const std::exception& e) {
throw AnException(e.what()
+ " raised from 'override_me()' "
+ " in 'run_some_operation()'");
}
}

C++ is not Java. The statement

e.what() + "..." + "...";

does not do what you think it does.


Yeah, I cobbled the above code together in a few minutes. I know I have do do
a "std::string(e.what()) + ...;

I'm a C++ programmer first, then a Java one if at all! :-)

--
To reply, take of all ZIGs !!
Feb 20 '06 #3
* Asfand Yar Qazi:

[idea of tacking on information to exception passing trough function]


You're not the first to think of that. We've all done it... ;-) With
macros and the whole shebang -- consider where the pressure to add
__func__ and so on comes from (it's the automated info-gathering).

Exception information is useful for mainly two purposes: debugging, and
logging.

Intercepting and rethrowing exceptions helps logging and "manual"
debugging, but interfere with interactive and JIT debuggers such as
those common in Windows programming (in particular, if you step over a
call and an exception occurs, you'd like the debugger to go right to
where that exception originated, not the latest rethrower, which is a
strong low-level-trenches argument for using RAII instead of try-catch).
Also, on the negative side, adding info makes exceptions less
efficient, and it can be very dangerous for that overly dangerous
exception std::bad_alloc. Although, of course, one may argue that the
only reasonable handling of std::bad_alloc is to install a new-handler
that logs the incident, says goodbye, and calls std::exit or std::abort.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 20 '06 #4
Alf P. Steinbach wrote:
* Asfand Yar Qazi:
>
> [idea of tacking on information to exception passing trough function]
You're not the first to think of that. We've all done it... ;-) With
macros and the whole shebang -- consider where the pressure to add
__func__ and so on comes from (it's the automated info-gathering).


lol. Actually, I was trying to get some input into whether or not Java's way
of 'nesting' exceptions as 'causes' was feasible.

Exception information is useful for mainly two purposes: debugging, and
logging.

Intercepting and rethrowing exceptions helps logging and "manual"
debugging, but interfere with interactive and JIT debuggers such as
those common in Windows programming (in particular, if you step over a
call and an exception occurs, you'd like the debugger to go right to
where that exception originated, not the latest rethrower, which is a
strong low-level-trenches argument for using RAII instead of try-catch).
Also, on the negative side, adding info makes exceptions less
efficient, and it can be very dangerous for that overly dangerous
exception std::bad_alloc. Although, of course, one may argue that the
only reasonable handling of std::bad_alloc is to install a new-handler
that logs the incident, says goodbye, and calls std::exit or std::abort.


Thanks - I will implement my own Exception base class (in fact, I already
have...) I wish there was a way to generate stack traces like in Java with
exceptions - out-side of the debugger of course.

I seem to remember in GCC you have to set a breakpoint to the function
_Unwind_RaiseException and you'd then be able to get a backtrace of where the
exception was about to be thrown.) Wish there was some way of getting
information like this in a standard manner.

--
To reply, take of all ZIGs !!
Feb 21 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Glen | last post by:
I have a Struts action form which contains a bean. I am trying to display a bean retrieved from the database in this form using the nested tag. Can anyone help me? I continue to get an error...
21
by: dkcpub | last post by:
I'm very new to Python, but I couldn't find anything in the docs or faq about this. And I fished around in the IDLE menus but didn't see anything. Is there a tool that can determine all the...
3
by: Erik Bongers | last post by:
Hi, Nested classes only seem to be able to access static members of the surrounding class : class SurroundingClass { public: class InnerClass { public:
2
by: newbiecpp | last post by:
Java can declare a static nested class. Does C++ have same thing like? class Outer { public: static class Inner { ... }; .... };
59
by: kk_oop | last post by:
Hi. I wanted to use exceptions to handle error conditions in my code. I think doing that is useful, as it helps to separate "go" paths from error paths. However, a coding guideline has been...
22
by: Drew | last post by:
How do I know which exceptions are thrown by certain methods? For example, reading a file might throw an IO Exception, etc. In Java, the compiler won't even let you compile unless you put your...
13
by: PeterZ | last post by:
Hi, Back to basics! My understanding is that the only way to exit a For-Next loop prematurely is with the 'break' keyword. How are you supposed to do that if you're inside a Switch...
1
by: Chris Leffer | last post by:
Could anyone shows me a simple example of nested exceptions handling on vb.net? Or any articles that explains this concept, since I am getting difficulties to see when/how to use nested exceptions....
0
RedSon
by: RedSon | last post by:
Chapter 3: What are the most common Exceptions and what do they mean? As we saw in the last chapter, there isn't only the standard Exception, but you also get special exceptions like...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.