Exceptions design problem

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"))
 
R

Reg Edit

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");
}
}


}
 
I

Ignacio Machin \( .NET/ C# MVP \)

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top