Re: Matching throw to catch at compile time?
On Feb 14, 8:16 am, "Patrik Kahari" <patrik.kah...@googlemail.com>
wrote:
I'm sure it makes sense since clever people came up with it. The thing
is; I don't understand why a throw can't be matched to the
corresponding (try and) catch clause at compiler time.
When exception occurs appropriate "catch" block matched taking into
consideration exception inheritance. Exceptions can be re-thrown.
It can lead to complicated exception handling examples like one below.
It is expected that if exception is not handled locally inside
function where it was generated for example - caller will be
responsible
for handling it. As result when unit of code compiled not all "throw"
statements can be matched with corresponding "catch" statements.
Sometimes it creates problems for users of libraries that generate
exceptions. Situation even worth since C++ exception specifications
are ignored so even if you say:
void f() throw(Exception_B) { f1(); }
it does not guarantee that only Exception_B can be generated inside
f().
Due to these reasons as well exceptions cannot be matched with
handling statements at compile time. This is not just C++ where
exceptions
handled this way - other languages like Java, C# and even Ada
have exceptions implemented in similar manner and they all rely on
runtime
mechanisms. This is C++ group, but exception handling goes far beyond
single programming language. For example think what happen if you
call
Ada library from C++ that trows (Ada) exceptions? What about
"exceptions"
generated by OS or hardware?
--- cut ---
try
{
try
{
srand(time(0));
if (rand()%2 == 0)
C::f0();
else
C::f1();
}
catch (const Exception_A& ex)
{
std::cout << "Exception_A\n";
throw; // Re-throw exception again
}
}
catch (const Exception_B& ex)
{
std::cout << "Exception_B\n";
}
catch (const Exception_Base& ex)
{
std::cout << "external Exception_Base\n";
}
catch(...)
{
std::cout << "external ...\n";
}
--- cut ---
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]