more information from catch(...)

K

Khuong Dinh Pham

How can i get more information about the exception when using catch(...)?


Thx in advance
 
I

Ian

Khuong said:
How can i get more information about the exception when using catch(...)?
You can't.

You have to try a catch specific typed first, like

catch( const std::exception& e )
{
}
catch( ... )
{
}

Ian
 
A

Alf P. Steinbach

* Khuong Dinh Pham:
How can i get more information about the exception when using catch(...)?

You can rethrow the exception (using "throw;") and catch it again with more
specific catch clauses.
 
O

Owen Jacobson

* Khuong Dinh Pham:

You can rethrow the exception (using "throw;") and catch it again with more
specific catch clauses.

Of course, if you're going to do that you might as well catch the specific
exceptions in the first place... Unless I'm missing something important,
which is entirely possible.

-O
 
A

Alf P. Steinbach

* Owen Jacobson:
Of course, if you're going to do that you might as well catch the specific
exceptions in the first place... Unless I'm missing something important,
which is entirely possible.

Yes, the possibility of centralizing the exception type determination and
relevant info extraction in a function, thus reducing code redundancy.
 
N

Nick Keighley

Alf said:
* Owen Jacobson:

Yes, the possibility of centralizing the exception type determination and
relevant info extraction in a function, thus reducing code redundancy.

hmm. that sounds like something I want to do. Could you expand on this
a bit?
I'd like to put a bit long list of catches in a function but quite see
how to
do that.
 
R

Rolf Magnus

Alf said:
* Owen Jacobson:

Yes, the possibility of centralizing the exception type determination and
relevant info extraction in a function, thus reducing code redundancy.

Why would I need to catch the exception in the first place if the only thing
to do with it is to throw it again?
 
A

Alf P. Steinbach

* Nick Keighley:
hmm. that sounds like something I want to do. Could you expand on this
a bit?
I'd like to put a bit long list of catches in a function but quite see
how to
do that.

Trust me, you don't _want_ to do that. But if you're using a bunch of
libraries that can thow various kinds of non-standard exceptions it can be a
more clean approach than the alternatives. It goes like this, off the cuff:

std::string exceptionText()
{
try
{
throw;
}
catch( unsigned x )
{
return "tulso tones exception #" + stringFrom( x );
}
catch( CExpectNot* pOuch )
{
std::string const result =
"Mucho Fishy Casserole exception: " + pOuch->Massage();
pOuch->Destroy();
return result;
}
catch( std::exception const& x )
{
return x.what();
}
catch( ... )
{
return "Unknown exception";
}
}

void foo()
{
try
{
lib1Func(); lib2Func(); lib3Func();
}
catch( ... )
{
throw std::runtime_error( exceptionText() );
}
}

and then bar(), similar, and so on.

This technique won't work with version 6.0 and earlier of MSVC, because of a
compiler bug.
 
A

Alf P. Steinbach

* Rolf Magnus:
Why would I need to catch the exception in the first place if the only thing
to do with it is to throw it again?

See my reply to Nick Keighley.
 
R

Roland Pibinger

It goes like this, off the cuff:

std::string exceptionText()
{
try
{
throw;
}
catch( unsigned x )
{
return "tulso tones exception #" + stringFrom( x );
}
catch( CExpectNot* pOuch )
{
std::string const result =
"Mucho Fishy Casserole exception: " + pOuch->Massage();
pOuch->Destroy();
return result;
}
catch( std::exception const& x )
{
return x.what();
}
catch( ... )
{
return "Unknown exception";
}
}

void foo()
{
try
{
lib1Func(); lib2Func(); lib3Func();
}
catch( ... )
{
throw std::runtime_error( exceptionText() );
}
}

and then bar(), similar, and so on.

Great idiom. Thank you!

1. You can safely add an exception specification, e.g.
void foo() throw (std::runtime_error)

2. A variant of this idiom has recently been posted here:
http://article.gmane.org/gmane.comp.lib.boost.devel/132551

Best wishes,
Roland Pibinger
 

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
473,997
Messages
2,570,241
Members
46,832
Latest member
UtaHetrick

Latest Threads

Top