Beginner on exception handling

A

andreas.koestler

Why does catch (A ex) rather than catch (B &ex) catch the B()
exception object? If I change the catch statement to catch (A &ex) it
doesn't catch B() anymore but catch (B &ex) does.
This might be quite a beginner question but you know, there's no
stupid questions just ....
Thanks Andreas

class A {
public:
A () {
std::cout << "A::A()" << std::endl;
}
virtual void Foo () {
std::cout << "A::Foo()" << std::endl;
}
};
class B : public A {
public:
B () {
std::cout << "B::B()" << std::endl;
}
virtual void Foo () {
std::cout << "B::Foo()" << std::endl;
}
};
int main(int argc, char** argv) {
try {
throw B();
}
catch ( A ex ) { //catch ( A &ex )
ex.Foo ();
}
catch ( B& ex ) {
ex.Foo ();
}
 
A

Alf P. Steinbach

* (e-mail address removed):
Why does catch (A ex) rather than catch (B &ex) catch the B()
exception object?

Because it's the first matching catch clause.

If I change the catch statement to catch (A &ex) it
doesn't catch B() anymore but catch (B &ex) does.

That's a misunderstanding on your part.

Add some output in the catch clauses.

This might be quite a beginner question but you know, there's no
stupid questions just ....
Thanks Andreas

class A {
public:
A () {
std::cout << "A::A()" << std::endl;
}
virtual void Foo () {
std::cout << "A::Foo()" << std::endl;
}
};
class B : public A {
public:
B () {
std::cout << "B::B()" << std::endl;
}
virtual void Foo () {
std::cout << "B::Foo()" << std::endl;
}
};
int main(int argc, char** argv) {
try {
throw B();
}
catch ( A ex ) { //catch ( A &ex )
ex.Foo ();
}
catch ( B& ex ) {
ex.Foo ();
}

Cheers & hth.,

- Alf
 
R

red floyd

Why does catch (A ex) rather than catch (B &ex) catch the B()
exception object? If I change the catch statement to catch (A &ex) it
doesn't catch B() anymore but catch (B &ex) does.
This might be quite a beginner question but you know, there's no
stupid questions just ....
Thanks Andreas

class A {
public:
    A () {
        std::cout << "A::A()" << std::endl;
    }
    virtual void Foo () {
        std::cout << "A::Foo()" << std::endl;
    }};

class B : public A {
public:
    B () {
        std::cout << "B::B()" << std::endl;
    }
    virtual void Foo () {
        std::cout << "B::Foo()" << std::endl;
    }};

int main(int argc, char** argv) {
    try {
        throw B();
    }
    catch ( A ex ) { //catch ( A &ex )
        ex.Foo ();
    }
    catch ( B& ex ) {
        ex.Foo ();
    }

You should catch your most derived exception class first.
 
N

Noah Roberts

Why does catch (A ex) rather than catch (B &ex) catch the B()
exception object? If I change the catch statement to catch (A &ex) it
doesn't catch B() anymore but catch (B &ex) does.
This might be quite a beginner question but you know, there's no
stupid questions just ....
Thanks Andreas

It's hard to understand what you're saying and what you're asking. I'll
just say what the code below should do and why:
class A {
public:
A () {
std::cout << "A::A()" << std::endl;
}
virtual void Foo () {
std::cout << "A::Foo()" << std::endl;
}
};
class B : public A {
public:
B () {
std::cout << "B::B()" << std::endl;
}
virtual void Foo () {
std::cout << "B::Foo()" << std::endl;
}
};
int main(int argc, char** argv) {
try {
throw B();
outputs
B::B()

}
catch ( A ex ) { //catch ( A &ex )
ex.Foo ();

outputs A::Foo()

If you replace with the commented code you see B::Foo()

The problem is that you're slicing your exception object. Always throw
by value, catch by reference.
}
catch ( B& ex ) {
ex.Foo ();

This code is simply never hit.

Try this:

int main()
{
B b;
A a1 = b;
A & a2 = b;

std::cout << a1.Foo() << std::endl;
std::cout << a2.Foo() << std::endl;
}
 
A

andreas.koestler

It's hard to understand what you're saying and what you're asking.  I'll
just say what the code below should do and why:






outputs A::Foo()

If you replace with the commented code you see B::Foo()

The problem is that you're slicing your exception object.  Always throw
by value, catch by reference.


This code is simply never hit.


Try this:

int main()
{
   B b;
   A a1 = b;
   A & a2 = b;

   std::cout << a1.Foo() << std::endl;
   std::cout << a2.Foo() << std::endl;

}

Thanks guys :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top