0

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.

2 Answers 2

2

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;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I would make DoRun protected, to emphasize that Run is the one outside code should be using.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.