Exceptions design problem

  • Thread starter Thread starter Julius Fuchs
  • Start date Start date
J

Julius Fuchs

Hi,

If i run the following program I get these messages: "show this", "caught",
"error", so the for loop in A is never completed
What I want is "show this", "caught", "error", "show this", "caught",
"error". How can I do that?


Here's my sample code:

private void StartHere()
{
try
{
new A();
}
catch
{
MessageBox.Show("error");
}
}

class A
{
public A()
{
for (int i = 0; i < 2; i++)
{
try
{
MessageBox.Show("show this");
new B();
MessageBox.Show("don't show this");
}
catch
{
MessageBox.Show("caught");
throw;
}
}
}
}

class B
{
public B()
{
throw new ApplicationException();
}
}

(B downloads a website, so there might occur an exception. A starts several
downloads (new B), if one fails the other should nevertheless be started
(the for loop must be completed) and the failed one be removed ("caught").
the first method reports the error to the user ("error"))
 
The throw from A exits the for loop.

If you want everything to happen twice, you could move the for loop up
outside the highest try/catch:

private void StartHere()
{
for (int i = 0; i < 2; i++)
{
try
{
new A();
}
catch
{
MessageBox.Show("error");
}
}


}
 
Hi,

What is what you want to do ?
The code you provide I assume is only to make your point, I don't think it's
the code y ou are really using right?
in any case, remove the throw in the catch inside the loop, that will solve
your problem.
 
Back
Top