Search code examples
c++exceptionlegacy

Pointer-to-Exception Clean-Up


We all know that throwing pointers to exception is bad:

try
{
    ...
    throw new MyExceptionClass();
}
catch (MyExceptionClass* e)
{
    ...
}

What's your approach to cleaning the catch targets up in legacy code? I figure that I can fix the first part by making operator new private:

class MyExceptionClass
{
public:
    ...
private:
    void* operator new(size_t);
}

How can I make the catch side of things equally ugly at compile-time? I don't want to just cause this to fall into the catch (...) territory.


Solution

  • If I understand you correctly, you want to turn a bad practice into a compilation error.

    By making the exception type non-heap-allocatable, you've managed to make this illegal:

    throw new MyExceptionClass();
    

    Alas, the next part can't be done like you want it. There's no way to make the catch block illegal. Although, if you've made it illegal to heap-allocate MyExceptionClass, there's no need to worry about the catch blocks. It'll just be wasted space.

    If you want to enforce not catching by a pointer, you want a lint-like tool. I'd recommend looking at EDoC++. It's a modified gcc compiler to check for proper exception usage.