mdjdrn1 mdjdrn1 - 2 months ago 8
C++ Question

Calling method from own exception class catched by its base class

I have two functions

a()
and
b()
, which are having own exception classes (consecutively
a_exc
and
b_exc
) that inherit from
std::logic_error
.

void a() { (...) throw a_exc(some_val) }
void b() { (...) throw b_exc(some_val) }

class a_exc : public std::logic_error
{
private:
int foo;
public:
a_exc(int val, const std::string& what_msg="Msg.")
: std::logic_error(what_msg), foo(val) {}
void show() { //show foo }
}

class b_exc : public std::logic_error
{
private:
std::string bar;
public:
a_exc(std::string val, const std::string& what_msg="Msg.")
: std::logic_error(what_msg), bar(val) {}
void show() { //show bar }
}


Let's say I have following part of code:

try {
a();
b();
}
catch (const std::logic_error& e)
{
e.what();
// e.show();
}


catch (const std::logic_error& e)
catches both
a_exc
and
b_exc
. Of course this block cannot use
e.show()
, because catched obj is
std::logic_error
.

And here's my problem. I wonder if there is any chance to call
show()
method in
std::logic_error
catch block when catched exception was
a_exc
or
b_exc
. I know, calling
show()
is possible if I create separate catch blocks for
a_exc
and
b_exc
, but I want to call this method with using just one catch block. Is it possible?

Answer

You can, provided that show() is a const member function:

catch (const std::logic_error& e)
{
    e.what();
    if(const a_exc* a = dynamic_cast<const a_exc*>(&e))
        a->show();
    else if(const b_exc* b = dynamic_cast<const b_exc*>(&e))
        b->show();
}

See it Live on Coliru. Though, it's usually a bad idea to call other functions that may throw in your catch exception handler.

Comments