3
\$\begingroup\$

Say, there is a class with some methods, that have try catch blocks. I need to accumulate messages from all inner exceptions of generated exception and use throw new Exception(exceptionMessages).
I came up with two possible solutions:

  1. Create a private method, concatenate inner exception messages, return a string and raise an exception in a caller method. (SomeMethod1 below);
  2. Create a private method, concatenate inner exception messages and raise exception inside it. (SomeMethod2 below);

Code snippet:

class SomeClass
{
    private void _raiseException(Exception ex)
    {
        var exceptionMessages = new StringBuilder();
        do
        {
            exceptionMessages.Append(ex.Message);
            ex = ex.InnerException;
        }
        while (ex != null);
        throw new Exception(exceptionMessages.ToString());
    }

    private string _getInnerExceptions(Exception ex)
    {
        var exceptionMessages = new StringBuilder();
        do
        {
            exceptionMessages.Append(ex.Message);
            ex = ex.InnerException;
        }
        while (ex != null);
        return exceptionMessages.ToString();
    }

    public void SomeMethod1()
    {
        try
        {
            //some code to try
        }
        catch (Exception e)
        {
            throw new Exception(this._getInnerExceptions(e));
        }
    }

    public void SomeMethod2()
    {
        try
        {
            //some code to try
        }
        catch (Exception e)
        {
            this._raiseException(e);
        }
    }
}

Which one of them would be better, or is there another way of doing it, or maybe this approach is not good at all?

\$\endgroup\$
  • \$\begingroup\$ Both approaches are the same but I would preferred 1st one (SomeMethod1()) - it's more clear that you are rethrowing exception and not just swallowing it \$\endgroup\$ – user1614543 Apr 23 '13 at 20:37
  • 2
    \$\begingroup\$ Why don't you use AggregateException instead? \$\endgroup\$ – svick Apr 23 '13 at 20:40
4
\$\begingroup\$

Svick is right

Use the AggregateException class to collect all exceptions you need. With AggregateException everyone can prepare it's own code to handle the multiple exception situation but with your original solution they have to handle a huge string in a stock exception class. How can be this usefull?

Storing exception.Message

It's bad. If you store only the exceptions' messages you will loose a lot of information and the types of the exceptions. How would you handle an exception if only the Message is what you have? Parsing it? And we havent talked about the specific properties of specific exceptions like some validation exception which can contain the unvalid property name and value for an object.

\$\endgroup\$
  • \$\begingroup\$ Thanks for pointed that out, I'm implementing the AggregateException then. \$\endgroup\$ – Artyom Neustroev Apr 24 '13 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.