chained exception

J

josh

Hi,
is there a standard way to "read" an exception rethrowed by a catch
and catch in an another extern try/catch?

Thanks
 
P

peter koch

Hi,
is there a standard way to "read" an exception rethrowed by a catch
and catch in an another extern try/catch?

Thanks

The idiom is something like this:

void detect_exception()
{
try
{
throw;
}
catch (std::exception const& se)
{
// std::exception thrown
}
catch (myexception const& me)
{
// myexception thrown
}
catch (...)
{
// something else thrown
}
}

/Peter
 
S

Sylvester Hesp

josh said:
Hi,
is there a standard way to "read" an exception rethrowed by a catch
and catch in an another extern try/catch?

Thanks

Sure there is, just use nested try/catch statements

#include <iostream>
int main()
{
try
{
try
{
std::cout << "throwing..." << std::endl;
throw 42;
}
catch(int i)
{
std::cout << "Inner catch: " << i << " - rethrowing..." <<
std::endl;
throw;
}
}
catch(int i)
{
std::cout << "Outer catch: " << i << std::endl;
}
}

Of course, the inner and outer catches don't have to be in the same
function, and the outer catch can rethrow again if deemed necessary.

- Sylvester
 
J

josh

Sure there is, just use nested try/catch statements

#include <iostream>
int main()
{
try
{
try
{
std::cout << "throwing..." << std::endl;
throw 42;
}
catch(int i)
{
std::cout << "Inner catch: " << i << " - rethrowing..." <<
std::endl;
throw;
}
}
catch(int i)
{
std::cout << "Outer catch: " << i << std::endl;
}

}

Of course, the inner and outer catches don't have to be in the same
function, and the outer catch can rethrow again if deemed necessary.

- Sylvester

yes but how can I do if I want to save the previous exception object.
In Java in a catch block I can do:

....
catch(Exception e)
{
throw new Exception("new exception", e)
}

and then the stack trace give me all the exceptions (also who has
caused)

Josh
 
P

peter koch

yes but how can I do if I want to save the previous exception object.
In Java in a catch block I can do:

...
catch(Exception e)
{
throw new Exception("new exception", e)

You could create your own exception class for this, but I really do
not see much purpose in that. Rather, I'd create an exception that
derives from both the exceptions you want to combine. And throw thast
one - e.g.

class file_hardware_exception: public file_exception, public
hardware_exception ...
and then the stack trace give me all the exceptions (also who has
caused)

You do not need a stack trace for exception handling. Stack traces are
valuable only when you are debugging stuff, and C++ exceptions were
never meant to be an aid in debugging.
If you'd like to see where an exception is generated, create your own
exception-class and put a breakpoint in the constructor.

/Peter
 
F

faceman28208

Hi,
is there a standard way to "read" an exception rethrowed by a catch
and catch in an another extern try/catch?

Other posters have shown most of the points. The only 2 cents I would
like to add is the opinion that *ALL* exception object should be of a
class type derived from std::exception..

If this is done consistently, any exception handler can determine the
name of the exception. Then you can do things like:


catch (std::exception &ee)
{
throw MyException (std::string ("Error establishing network
connection." + "\n" + ee.what ()) ;
}

This is also why think "throw" specifications are the single most
worthless "feature" added to C++ during the standardization process.
 
J

James Kanze

On May 16, 3:54 am, josh <[email protected]> wrote:

[...]
Other posters have shown most of the points. The only 2 cents I would
like to add is the opinion that *ALL* exception object should be of a
class type derived from std::exception..

It depends. It's generally not a good idea to call exit() in a
C++ program, so I occasionally use the convention that the user
should throw an int instead, and that main should basically look
like:

int
main()
{
try {
// the real code...
} catch ( int returnCode ) {
return returnCode ;
}
}
If this is done consistently, any exception handler can determine the
name of the exception. Then you can do things like:
catch (std::exception &ee)
{
throw MyException (std::string ("Error establishing network
connection." + "\n" + ee.what ()) ;
}

And how does that help? In my experience, "what()" rarely
returns anything useful.

The real problem is that you currently have no real way of
cloning the actual exception object. Which means that you can't
really implement the chaining yourself, and (a more serious
problem, IMHO), you can't implement anything to support passing
the exception accross a join in a multithreaded environment.
 
F

faceman28208

And how does that help? In my experience, "what()" rarely
returns anything useful.

That would be programming incompetence. To define a class derived from
std::exception whose _what()_ does not return anything useful, shows a
lack of programming skill.


The one problem of throwing an int would be someone adding a throw
specification to some function, something that wold prevent your int
value from getting to the top level.
 
J

James Kanze

That would be programming incompetence. To define a class derived from
std::exception whose _what()_ does not return anything useful, shows a
lack of programming skill.

Have you actually looked at what what() returns in any of the
standard libraries? I don't totally disagree with your
statement, but from experience...
The one problem of throwing an int would be someone adding a throw
specification to some function, something that wold prevent your int
value from getting to the top level.

It's definitly a technique that should be restricted to
application level code. Where you are in complete control.
(Most coding guidelines I've seen ban exception specifications,
except for empty ones.)
 

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

No members online now.

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,201
Latest member
JefferyBur

Latest Threads

Top