Frage

I have decided to use Exceptions in my code to pass error handling around. I found myself duplicating code each time I wanted to create a new exception. These classes were nothing special and only contained a messaged. But I have come to rely on type safety when handing them. Is there a way to provide a new exception class type without having to re-implement the constructors?

[Serializable]
class MyNewException : MyBaseException
{
    public MyNewException (String tMsg)
        : base(tMsg)
    {
    }

    public MyNewException (String tMsg, Exception tInnerEx)
        : base(tMsg, tInnerEx)
    {
    }
}

The code above is duplicated many times over for each different type of exception I want to define.

War es hilfreich?

Lösung

Unfortunately, no, the constructors have to be provided since they are not inherited.

In addition, unless you are catching these specific exceptions and performing explicit processing when they occur, I would recommend having a generic exception that contains the additional information that you might need. However, this may not apply in your case.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top