List of Exceptions

Asked

Viewed 884 times

3

How to implement a class so that I can add an Exception to an Exception list.

This class would be used for the case below, for example:

In the validation method can return one or more errors, so each error found I will add an Exception in the list and at the end of the validation triggers this list that will fall within the specific catch of the type of this list.

  • 1

    You don’t want to use AggregateException? Can create a AggregateException with a list of exceptions and then catch the AggregateException in a catch and access the exceptions through the .InnerExceptions.

  • 2

    A validation method should not make an exception. http://answall.com/q/21767/101 http://answall.com/q/15261/101 http://answall.com/a/56299/101

  • 1

    @Leandro a tip is to be careful about releasing exceptions frequently , depending on what is its validation , because if it is for example validation of a registration and there are frequent errors this can bring down the app pool in IIS , even if the exception is treated . ( IIS understands that this should happen if many exceptions occur )

  • Thanks for the tip @Johndiego. But in my case is for Windows Form same.

3 answers

5


Can use the AggregateException to do what you want:

try
{
    List<Exception> excepcoes = new List<Exception>();
    excepcoes.Add(new ArgumentException("argumento", "argumento invalido"));
    excepcoes.Add(new ArgumentNullException("argumento nulo"));
    excepcoes.Add(new InvalidCastException("operacao invalida"));

    throw new AggregateException(excepcoes);
}
catch(AggregateException aEx)
{
    foreach(var ex in aEx.InnerExceptions)
    {
        Console.WriteLine("{0}: {1}\n", ex.GetType(), ex.Message);

        // Output:

        // System.ArgumentException: argumento
        // Parameter name: argumento invalido

        // System.ArgumentNullException: Value cannot be null.
        // Parameter name: argumento nulo

        // System.InvalidCastException: operacao invalida
    }
}

The constructor of AggregateException accepts a list of exceptions, which can then be accessed through the property .InnerExceptions.

Behold here an example in dotFiddle.

  • Eita confused the question... read again ! Your solution is the right one !

  • If you need to report exceptions, a list of exceptions will not work. You need to involve them in some other kind of exception that contains them. Your answer works if you have to access the exceptions where they are raised. But consider that there is a method where exceptions occur and that a) need to accumulate exceptions, b) need to raise an exception if there is an exception and c) need to communicate all exceptions. In this case, a AggregateException or similar and the viable alternative (return exceptions presumes that there are always exceptions).

  • 1

    Show!!! Didn’t know that Aggregateexception does exactly what I want.

1

You create the list like any other...

Following example: fiddle

        List<Exception> le = new List<Exception>();

        try
        {
            try{
                throw new Exception(" except 1");
            }
            catch(Exception e)
            {
                le.Add(e);
                throw new Exception("except 2");
            }
        }
        catch(Exception e)          
        {
            le.Add(e);
        }

        foreach(var e in le)
        {
             Console.WriteLine(e);
        }
  • You could do that but that’s not what I want, because each error would have to fire Exception and then add it to the list inside the catch. The @Omni example fits me perfectly.

0

You can create your own Exception by inheriting from Exception, in it, you add an array of Exception or its Exception, and throw / catch yours. when to give the catch you can show all exceptions from the list, if they exist.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.