 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
mathiasn Guest
|
Posted: Wed May 17, 2006 3:21 pm Post subject: Rethrow from other place than catch-block |
|
|
Hi,
Is it legal to rethrow an exception from a function called from the
catch block?
Is the following program legal C++?
#include <iostream>
using namespace std;
void ExceptionHandler(const char* context)
{
cout << "In ExceptionHandler(" << context << "):";
try // Setup a new try-block and rethrow the exception
{
throw;
}
catch (const int& e)
{
cout << "int = " << e << "\n";
}
catch (const char* e)
{
cout << "text = " << e << "\n";
}
catch (...)
{
cout << "UNKNOWN\n";
}
}
int _tmain(int argc, _TCHAR* argv[])
{
for (int i = 0; i < 5; ++i)
{
try
{
switch (i)
{
case 0: throw 42;
case 1: throw "Some text";
case 2: throw 12.4;
case 3: cout << "Still alive\n"; break;
case 4:
{
try
{
throw "Another text";
}
catch (...)
{
ExceptionHandler("Other context");
}
}
break;
}
}
catch (...) // Catch all exceptions here
{
ExceptionHandler("Inside for loop");
}
}
return 0;
}
This small testprogram compiles and runs as expected with Visual C++
2005. But in our bigger system we are expecting that code like this is
causing problems.
Best regards
Mathias
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
red floyd Guest
|
Posted: Wed May 17, 2006 10:21 pm Post subject: Re: Rethrow from other place than catch-block |
|
|
mathiasn wrote:
| Quote: | Hi,
Is it legal to rethrow an exception from a function called from the
catch block?
Is the following program legal C++?
|
Technically, no, because _TCHAR is undefined and there is no main.
That said, assuming MS usage, yes, it appears to be legal. 15.1/6-8.
| Quote: | #include <iostream
using namespace std;
void ExceptionHandler(const char* context)
{
cout << "In ExceptionHandler(" << context << "):";
try // Setup a new try-block and rethrow the exception
{
throw;
}
catch (const int& e)
{
cout << "int = " << e << "\n";
}
catch (const char* e)
{
cout << "text = " << e << "\n";
}
catch (...)
{
cout << "UNKNOWN\n";
}
}
int _tmain(int argc, _TCHAR* argv[])
{
for (int i = 0; i < 5; ++i)
{
try
{
switch (i)
{
case 0: throw 42;
case 1: throw "Some text";
case 2: throw 12.4;
case 3: cout << "Still alive\n"; break;
case 4:
{
try
{
throw "Another text";
}
catch (...)
{
ExceptionHandler("Other context");
}
}
break;
}
}
catch (...) // Catch all exceptions here
{
ExceptionHandler("Inside for loop");
}
}
return 0;
}
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Wed May 17, 2006 10:21 pm Post subject: Re: Rethrow from other place than catch-block |
|
|
* mathiasn:
| Quote: |
Is it legal to rethrow an exception from a function called from the
catch block?
|
Yes, but it's not supported by some older compilers, in particular, it's
not supported by Visual C++ 6.0 and earlier (a compiler bug wrt.
destructor calls).
| Quote: | Is the following program legal C++?
|
No, because
1) it's lacking
#include <ostream>
;-)
2) it's using a non-standard startup function '_tmain'; of course
that's meant to be defined as a preprocessor symbol 'main' when
building the program, but it won't compile without such def.
| Quote: | #include <iostream
using namespace std;
void ExceptionHandler(const char* context)
{
cout << "In ExceptionHandler(" << context << "):";
try // Setup a new try-block and rethrow the exception
{
throw;
}
catch (const int& e)
{
cout << "int = " << e << "\n";
}
catch (const char* e)
{
cout << "text = " << e << "\n";
}
catch (...)
{
cout << "UNKNOWN\n";
}
}
int _tmain(int argc, _TCHAR* argv[])
{
for (int i = 0; i < 5; ++i)
{
try
{
switch (i)
{
case 0: throw 42;
case 1: throw "Some text";
case 2: throw 12.4;
case 3: cout << "Still alive\n"; break;
case 4:
{
try
{
throw "Another text";
}
catch (...)
{
ExceptionHandler("Other context");
}
}
break;
}
}
catch (...) // Catch all exceptions here
{
ExceptionHandler("Inside for loop");
}
}
return 0;
}
This small testprogram compiles and runs as expected with Visual C++
2005. But in our bigger system we are expecting that code like this is
causing problems.
|
I would also expect that, because the function breaks a golden rule of
design: separation of concerns.
Let the function return the information needed to process the exception,
e.g., some descriptive text, or let it rethrow the exception with some
project-standard exception type.
Don't let it process the exception: it doesn't know what's appropriate.
--
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?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Sergey P. Derevyago Guest
|
Posted: Wed May 17, 2006 10:21 pm Post subject: Re: Rethrow from other place than catch-block |
|
|
mathiasn wrote:
| Quote: | Is it legal to rethrow an exception from a function called from the
catch block?
It must be legal. |
In particular, ARM contains the following example:
void pass_through() { throw; }
void g()
{
PFV old = set_unexpected(&pass_through);
try { f(); }
catch (X) { }
catch (Y) { }
catch (...) { }
set_unexpected(old);
}
And my Russian "Interfaces and Messages" tutorial uses this technique to
implement common newCException() handler:
void f()
{
try { g(); }
catch (...) {
throw newCException(_FLINE_, "Problems in f()", toCException(_FLINE_));
}
}
sh_ptr<CException> toCException(const CException::FileLine& location)
{
#ifndef DERS_RETHROW_BUG // normal version
try { throw; }
catch (sh_ptr<CException> ce) {
return ce;
}
catch (const exception& e) {
return newSTDExternalCException(location, e.what(), typeid(e).name());
}
catch (...) {
return newUnknownExternalCException(location, "Unknown exception",
"unknown");
}
#else // version for broken compilers
if (CException::current.refs()>1 && CException::current.get())
return CException::current;
return newUnknownExternalCException(location, "Unknown exception",
"unknown");
#endif
}
You can download the source code http://ders.angen.net/cpp3/intmsg/code.zip
and run ceexample.cpp
| Quote: | This small testprogram compiles and runs as expected with Visual C++
2005. But in our bigger system we are expecting that code like this is
causing problems.
Yes, it does. |
Unfortunately, I have to define DERS_RETHROW_BUG macro if the nested
rethrowing isn't supported by a particular compiler.
--
With all respect, Sergey. http://ders.angen.net/
mailto : ders at skeptik.net
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
David Abrahams Guest
|
Posted: Wed May 17, 2006 10:21 pm Post subject: Re: Rethrow from other place than catch-block |
|
|
"mathiasn" <mathias.nord (AT) gmail (DOT) com> writes:
| Quote: | Hi,
Is it legal to rethrow an exception from a function called from the
catch block?
Is the following program legal C++?
|
Yes and yes.
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Andrew Koenig Guest
|
Posted: Wed May 17, 2006 10:21 pm Post subject: Re: Rethrow from other place than catch-block |
|
|
"mathiasn" <mathias.nord (AT) gmail (DOT) com> wrote in message
news:1147857637.106720.258710 (AT) j33g2000cwa (DOT) googlegroups.com...
| Quote: | Is it legal to rethrow an exception from a function called from the
catch block?
|
Yes.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Hyman Rosen Guest
|
Posted: Wed May 17, 2006 10:21 pm Post subject: Re: Rethrow from other place than catch-block |
|
|
mathiasn wrote:
| Quote: | Is it legal to rethrow an exception from a function called from the
catch block?
|
Yes, and it's useful for consolidating repetitive
handler blocks as your code demonstrates.
For a long time, Visual C++ would crash when you
did this, so many people don't use this construct.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|