0

I have two classes, A, and B. They are declared thusly:

class A
{
    public: void function() throw (exception);
};

class B
{
    public: void function();
};

B::function calls A::function. Inside B::function, I want to suppress the exception that A::function sometimes throws, and continue execution afterwards. How do I do this?

Wug
  • 12,956
  • 4
  • 34
  • 54
user1559792
  • 347
  • 2
  • 6
  • 15
  • Well, you can just wrap every call to a method from `A` with `try { } catch (...) {}` to swallow the exception. That will work, but it's generally a bad idea to ignore exceptions. – j_random_hacker Nov 02 '12 at 13:36
  • Can you try to post real code? What you posted will not compile since `A::function` will be private and `B` isn't a `friend` of `A`. – Benjamin Bannier Nov 02 '12 at 13:48
  • Throw in the function signature is a bad practice (http://stackoverflow.com/questions/1055387/throw-keyword-in-functions-signature-c), and throwing things besides std::exceptions is a bad practice. – Robert Cooper Nov 02 '12 at 13:49

1 Answers1

4

You can drop all exceptions using try { .. } catch ( ... ) { }:

void ClassB::doSomething()
{
    try {
        classAObject.doSomethingWhichMayThrow();
    } catch ( ... ) {
    }
}

Please note that this may have moral implications. You should be ready to explain (at least in a comment of the code) why swallowing exceptions at this point is acceptable.

Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
  • I still have the problem. I write an example to show. http://codepad.org/4cMBEYHg http://codepad.org/d5Epig9R http://codepad.org/Ix1zjrIK http://codepad.org/LWlFPjni when I'm compiling, it stops. – user1559792 Nov 02 '12 at 15:37