On 2008-08-26 12:44:48 -0400, Christian Hackl <ha***@sbox.tugraz.atsaid:
red floyd wrote:
>Per 15.1/8, "If no exception is presently being handled, executing a /
throw-expression/ with no operand calls terminate()."
In other words, a "throw;" must occur inside a catch block, or you
will be terminated.
I think that's too narrow. An exception can be handled even if you are
not currently inside of a catch block, can't it?
It depends on just what you mean by "inside" a catch block. Most people
would say that the throw; below is inside a catch block. If you call
handleException directly from main, i.e. not inside any catch block,
the program will, indeed, be terminated.
>
#include <exception>
#include <stdexcept>
#include <iostream>
void handleException()
{
try
{
throw;
}
catch (std::exception const &exc)
{
std::cout << exc.what() << "\n";
}
catch (...)
{
std::cout << "unknown exception\n";
}
}
int main()
{
try
{
throw std::runtime_error("test");
}
catch (...)
{
handleException();
}
}
Isn't this a perfectly standard-conforming program? It does not call
terminate() when compiled and run with VC or GCC.
--
Pete
Roundhouse Consulting, Ltd. (
www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(
www.petebecker.com/tr1book)