bge0
bge0

Reputation: 921

C++ Exception Handling In Base Class

Supposing I have some general Application class definition which contains:

class Application{
    public:
    void Run(){
        try{
            ....
        }catch(const Exception& ex){
            ....
        }
}

Now supposing I inherit Application from class App1:

class App1: public Application{
    void Run(){
        Application::Run();
        throw ex;
    }
}

Is there a way to catch the exception in the base class? I tried the above and it doesn't work.

Upvotes: 0

Views: 63

Answers (2)

Slava
Slava

Reputation: 1095

Using virtual function, like this:

    class Application{
    public:
    virtual void DoRun() {
    ....
    }
    void Run(){
        try{
            DoRun();
        }catch(const Exception& ex){
            ....
        }
}

class App1: public Application{
    virtual void DoRun() {
        Application::DoRun();
        throw ex;
    }
}

Upvotes: 2

R Sahu
R Sahu

Reputation: 206727

No, you can't. By the time you throw ex; in App1:Run(), Application::Run() has already finished running.

Upvotes: 0

Related Questions