473,327 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Exceptions design problem

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"))
Aug 10 '06 #1
2 1059
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");
}
}
}

Aug 10 '06 #2
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.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Julius Fuchs" <ju**********@yahoo.dewrote in message
news:1p*****************************@40tude.net...
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"))

Aug 10 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: OvErboRed | last post by:
I just read a whole bunch of threads on microsoft.public.dotnet.* regarding checked exceptions (the longest-running of which seems to be <cJQQ9.4419 $j94.834878@news02.tsnz.net>. My personal...
24
by: mag31 | last post by:
Is there any way to find out if a particular .net function will throw an exception without first generating the exception? I am using structured exception handling i.e. try catch finally blocks...
5
by: Mark Oueis | last post by:
I've been struggling with this question for a while. What is better design? To design functions to return error codes when an error occures, or to have them throw exceptions. If you chose the...
6
by: Iain | last post by:
Hey folks, (I posted this in microsoft.public.dotnet.csharp.general yesterday, but it appears that this group is rather more lively) For the application I am developing, I have a data access...
22
by: Drew | last post by:
How do I know which exceptions are thrown by certain methods? For example, reading a file might throw an IO Exception, etc. In Java, the compiler won't even let you compile unless you put your...
14
by: dcassar | last post by:
I have had a lively discussion with some coworkers and decided to get some general feedback on an issue that I could find very little guidance on. Why is it considered bad practice to define a...
8
by: cat | last post by:
I had a long and heated discussion with other developers on my team on when it makes sense to throw an exception and when to use an alternate solution. The .NET documentation recommends that an...
4
by: Steve | last post by:
I have read a couple articles online, read my Jesse Liberty book but I am still confused as to just what the best practices are for using exceptions. I keep changing how I'm working with them and...
6
by: Liming | last post by:
Hi, In a typical 3 tier model (view layer, busines layer and data access layer) where do you handle your exceptions? do you let it buble up all the way to the .aspx pages or do you handle it in...
13
by: mike3 | last post by:
Hi. (crossposted because the program is in C++ and some C++-related elements are discussed, hence comp.lang.c++, plus general program design questions are asked, hence comp.programming.) I'm...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.