-2

Is it possible to resume the execution of my Programm after the first catch even if i throwed the exception?

I made an example programm but the line where i added the exception to my List<string> has to be executed exactly after the inner and inside the outer foreach i know it would be possible if i wouldnt throw the exception but i have to do this aswell.

foreach(var x in x)
{
    try
    {
        List<string> exs = new List<string>();
        foreach(var a in b)
        {
            try
            {
                //...some code
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                //...some code
            }
        }
        exs.Add(ex);
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex);
    }
    finally
    {
        //...some code
    }
}
speschel
  • 46
  • 1
  • 8
  • 2
    Note that `throw ex` resets the stack track, use `throw` to keep the information where the exception really was raised. – Tim Schmelter Oct 12 '18 at 09:26
  • So you want to keep on executing the innerloop? – TheGeneral Oct 12 '18 at 09:28
  • 1
    A lot of pseudo code which doesn't compile. Why you don't provide code which we could copy&paste without having to fix compiler errrors that are not related to your problem? For example: `foreach(var x in x)` or `exs.Add(ex)`(ex is the exception) – Tim Schmelter Oct 12 '18 at 09:36
  • The code has errors, and apart from that it's unclear what you are trying to achieve. Which exceptions should be thrown, who should handle them, and what should the list contain at the end? – Mo B. Oct 12 '18 at 09:41

1 Answers1

0

Don't throw them, but collect and print them later.

List<Exception> exs = new List<Exception>();
try
{

    foreach(var a in b)
    {
        try
        {
            //...some code
        }
        catch(Exception ex)
        {
            exs.Add(ex);
        }
        finally
        {
            //...some code
        }
    }        
}    
catch(Exception e)
{
   Console.WriteLine(e.ToString());
}
finally
{
    //...some code
}
exs.ForEach(ex=> Console.WriteLine(ex.ToString()));
Access Denied
  • 8,723
  • 4
  • 42
  • 72