R
Richard Herring
annex <[email protected]> said:hi, i have some queries about handling exceptions, i'm using Borland c++
Builder 6.
1) what's the best way to catch multiple exceptions in one catch statement?
is this possible? for e.g i want to catch 2 exceptions; MyEx1 and MyEx2,
both to be handled in the same way. how to do this?
try
{
doSomething();
}
catch (MyEx1)
{
sameCode();
}
catch (MyEx2)
{
sameCode();
}
note that both exceptions are to be handled using the same piece of code.
Lots of answers, but nobody seems to have answered this directly.
Answer: derive both from a common base class and catch by reference:
class MyExBase {...}
class MyEx1: public MyExBase {...}
class MyEx2: public MyExBase {...}
try
{
doSomething();
}
catch (MyExBase &)
{
sameCode();
}
2) what's the difference btw catch(...) and catch(Exception). will
catch(Exception) catch all unhandled exceptions also like does catch(...) ?
No. But catch(Exception&) will catch anything derived from Exception.