try...catch...inside loop

R

Raj

This is a console application consuming a web service (Add). It works fine as
long as internet connectivity is there. In case of drop in internet
connectivity, i want to reattempt to consume the web service. As long as I
wish to continue, or once the web service is consumed the loop will
terminate. When I have problem with Internet Connectivity, the loop is not
iterating and Console.Read is skipped.

Any help would be appreciated

This is the output I got If I don't have internet connectivity:

The remote name could not be resolved: 'www.xxx.com'
Do you want to retry[Y/N]?
Y
Y
Out of try...catch, inside while
The remote name could not be resolved: 'www.digitechpower.com'
Do you want to retry[Y/N]?


Out of try...catch, inside while

Code Snippet:
static void Main(string[] args)
{
Service1 s = new Service1();
char cont='Y';

while(cont=='Y')
{
try
{
int result = s.Add(100, 200);
Console.WriteLine(result.ToString());
cont = 'N';
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Do you want to retry[Y/N]?");
cont = (Char)Console.Read();
Console.WriteLine(cont);
}
Console.WriteLine("Out of try...catch, inside while");
Console.Read();
}
Console.WriteLine("Reached End");
Console.Read();
}

Thank you

Regards
Raj
 
F

Family Tree Mike

Raj said:
This is a console application consuming a web service (Add). It works fine as
long as internet connectivity is there. In case of drop in internet
connectivity, i want to reattempt to consume the web service. As long as I
wish to continue, or once the web service is consumed the loop will
terminate. When I have problem with Internet Connectivity, the loop is not
iterating and Console.Read is skipped.

Any help would be appreciated

This is the output I got If I don't have internet connectivity:

The remote name could not be resolved: 'www.xxx.com'
Do you want to retry[Y/N]?
Y
Y
Out of try...catch, inside while
The remote name could not be resolved: 'www.digitechpower.com'
Do you want to retry[Y/N]?


Out of try...catch, inside while

Code Snippet:
static void Main(string[] args)
{
Service1 s = new Service1();
char cont='Y';

while(cont=='Y')
{
try
{
int result = s.Add(100, 200);
Console.WriteLine(result.ToString());
cont = 'N';
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Do you want to retry[Y/N]?");
cont = (Char)Console.Read();
Console.WriteLine(cont);
}
Console.WriteLine("Out of try...catch, inside while");
Console.Read();
}
Console.WriteLine("Reached End");
Console.Read();
}

Thank you

Regards
Raj

It looks to me as if the code is behaving as you coded it. It appears you
are pressing [enter] in your example, when asked to enter 'Y' or 'N', and
therefore you skip out of the loop (the second time). The first time, you
did hit 'Y'.

Mike
 
G

Gregory A. Beamer

This is a console application consuming a web service (Add). It works
fine as long as internet connectivity is there. In case of drop in
internet connectivity, i want to reattempt to consume the web service.
As long as I wish to continue, or once the web service is consumed the
loop will terminate. When I have problem with Internet Connectivity,
the loop is not iterating and Console.Read is skipped.

Any help would be appreciated

Create a separate routine that attempts the connection to the web
service. Have it throw an error or return a value. You can then place
the call to this routine in your while loop.

If it returns a value: terminate loop
If it returns an error: bring up message box
If user chooses to terminate, terminate
If user chooses not to, try again

This follows a much better model than what you have coded, as it
separates out the call from the loop a bit and uses the standard throw
an exception on failure model.

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
A

Anthony Tolle

This is a console application consuming a web service (Add). It works fine as
long as internet connectivity is there. In case of drop in internet
connectivity, i want to reattempt to consume the web service. As long as I
wish to continue, or once the web service is consumed the loop will
terminate. When I have problem with Internet Connectivity, the loop is not
iterating and Console.Read is skipped.

Any help would be appreciated

This is the output I got If I don't have internet connectivity:

The remote name could not be resolved: 'www.xxx.com'
Do you want to retry[Y/N]?
Y
Y
Out of try...catch, inside while
The remote name could not be resolved: 'www.digitechpower.com'
Do you want to retry[Y/N]?

Out of try...catch, inside while

Code Snippet:
static void Main(string[] args)
        {
            Service1 s = new Service1();
            char cont='Y';

            while(cont=='Y')
            {
                try
                {
                    int result = s.Add(100, 200);
                    Console.WriteLine(result.ToString());
                    cont = 'N';
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine("Do you want toretry[Y/N]?");
                    cont = (Char)Console.Read();
                    Console.WriteLine(cont);
                }
                Console.WriteLine("Out of try...catch, inside while");
                Console.Read();
            }
            Console.WriteLine("Reached End");
            Console.Read();
        }

Thank you

Regards
Raj

Here is what is happening:

On the first call to Console.Read, it blocks until you press Enter.
Before returning the "Y", the following characters are in the buffer:

['Y', CR, LF]

CR = carriage return (0x0d)
LF = line feed (0x0a)

Once Console.Read returns the 'Y', the buffer now contains [CR, LF].

The next Console.Read inside the while loop (outside the try/catch)
reads the CR, so now the buffer contains a single LF character.

Then, the next Console.Read inside the catch statement pulls the LF
character. Because the cont variable now equals LF and not 'Y', the
loop exits.

Because of problems like this, I would recommend following the advice
on the Console.Read documentation page (http://msdn.microsoft.com/en-
us/library/system.console.read.aspx):

"The ReadLine method, or the KeyAvailable property and ReadKey method
are preferable to using the Read method."
 
R

Raj

Even after I remov the 2nd and 3rd Console.Read()s, I don't have any respite!

Whereas, this peace of code is workin fine:
char name=' ';
while (name != '.')
{
name = (Char) Console.Read();
Console.Write(name);
}
Console.Read();



Family Tree Mike said:
Raj said:
This is a console application consuming a web service (Add). It works fine as
long as internet connectivity is there. In case of drop in internet
connectivity, i want to reattempt to consume the web service. As long as I
wish to continue, or once the web service is consumed the loop will
terminate. When I have problem with Internet Connectivity, the loop is not
iterating and Console.Read is skipped.

Any help would be appreciated

This is the output I got If I don't have internet connectivity:

The remote name could not be resolved: 'www.xxx.com'
Do you want to retry[Y/N]?
Y
Y
Out of try...catch, inside while
The remote name could not be resolved: 'www.digitechpower.com'
Do you want to retry[Y/N]?


Out of try...catch, inside while

Code Snippet:
static void Main(string[] args)
{
Service1 s = new Service1();
char cont='Y';

while(cont=='Y')
{
try
{
int result = s.Add(100, 200);
Console.WriteLine(result.ToString());
cont = 'N';
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Do you want to retry[Y/N]?");
cont = (Char)Console.Read();
Console.WriteLine(cont);
}
Console.WriteLine("Out of try...catch, inside while");
Console.Read();
}
Console.WriteLine("Reached End");
Console.Read();
}

Thank you

Regards
Raj

It looks to me as if the code is behaving as you coded it. It appears you
are pressing [enter] in your example, when asked to enter 'Y' or 'N', and
therefore you skip out of the loop (the second time). The first time, you
did hit 'Y'.

Mike
 
R

Raj

Even after I remov the 2nd and 3rd Console.Read()s, I don't have any respite!

Whereas, this peace of code is workin fine:
char name=' ';
while (name != '.')
{
name = (Char) Console.Read();
Console.Write(name);
}
Console.Read();



Anthony Tolle said:
This is a console application consuming a web service (Add). It works fine as
long as internet connectivity is there. In case of drop in internet
connectivity, i want to reattempt to consume the web service. As long as I
wish to continue, or once the web service is consumed the loop will
terminate. When I have problem with Internet Connectivity, the loop is not
iterating and Console.Read is skipped.

Any help would be appreciated

This is the output I got If I don't have internet connectivity:

The remote name could not be resolved: 'www.xxx.com'
Do you want to retry[Y/N]?
Y
Y
Out of try...catch, inside while
The remote name could not be resolved: 'www.digitechpower.com'
Do you want to retry[Y/N]?

Out of try...catch, inside while

Code Snippet:
static void Main(string[] args)
{
Service1 s = new Service1();
char cont='Y';

while(cont=='Y')
{
try
{
int result = s.Add(100, 200);
Console.WriteLine(result.ToString());
cont = 'N';
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Do you want to retry[Y/N]?");
cont = (Char)Console.Read();
Console.WriteLine(cont);
}
Console.WriteLine("Out of try...catch, inside while");
Console.Read();
}
Console.WriteLine("Reached End");
Console.Read();
}

Thank you

Regards
Raj

Here is what is happening:

On the first call to Console.Read, it blocks until you press Enter.
Before returning the "Y", the following characters are in the buffer:

['Y', CR, LF]

CR = carriage return (0x0d)
LF = line feed (0x0a)

Once Console.Read returns the 'Y', the buffer now contains [CR, LF].

The next Console.Read inside the while loop (outside the try/catch)
reads the CR, so now the buffer contains a single LF character.

Then, the next Console.Read inside the catch statement pulls the LF
character. Because the cont variable now equals LF and not 'Y', the
loop exits.

Because of problems like this, I would recommend following the advice
on the Console.Read documentation page (http://msdn.microsoft.com/en-
us/library/system.console.read.aspx):

"The ReadLine method, or the KeyAvailable property and ReadKey method
are preferable to using the Read method."
.
 
R

Raj

Even after I remov the 2nd and 3rd Console.Read()s, I don't have any respite!

Whereas, this peace of code is workin fine:
char name=' ';
while (name != '.')
{
name = (Char) Console.Read();
Console.Write(name);
}
Console.Read();



Anthony Tolle said:
This is a console application consuming a web service (Add). It works fine as
long as internet connectivity is there. In case of drop in internet
connectivity, i want to reattempt to consume the web service. As long as I
wish to continue, or once the web service is consumed the loop will
terminate. When I have problem with Internet Connectivity, the loop is not
iterating and Console.Read is skipped.

Any help would be appreciated

This is the output I got If I don't have internet connectivity:

The remote name could not be resolved: 'www.xxx.com'
Do you want to retry[Y/N]?
Y
Y
Out of try...catch, inside while
The remote name could not be resolved: 'www.digitechpower.com'
Do you want to retry[Y/N]?

Out of try...catch, inside while

Code Snippet:
static void Main(string[] args)
{
Service1 s = new Service1();
char cont='Y';

while(cont=='Y')
{
try
{
int result = s.Add(100, 200);
Console.WriteLine(result.ToString());
cont = 'N';
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Do you want to retry[Y/N]?");
cont = (Char)Console.Read();
Console.WriteLine(cont);
}
Console.WriteLine("Out of try...catch, inside while");
Console.Read();
}
Console.WriteLine("Reached End");
Console.Read();
}

Thank you

Regards
Raj

Here is what is happening:

On the first call to Console.Read, it blocks until you press Enter.
Before returning the "Y", the following characters are in the buffer:

['Y', CR, LF]

CR = carriage return (0x0d)
LF = line feed (0x0a)

Once Console.Read returns the 'Y', the buffer now contains [CR, LF].

The next Console.Read inside the while loop (outside the try/catch)
reads the CR, so now the buffer contains a single LF character.

Then, the next Console.Read inside the catch statement pulls the LF
character. Because the cont variable now equals LF and not 'Y', the
loop exits.

Because of problems like this, I would recommend following the advice
on the Console.Read documentation page (http://msdn.microsoft.com/en-
us/library/system.console.read.aspx):

"The ReadLine method, or the KeyAvailable property and ReadKey method
are preferable to using the Read method."
.
 
P

Peter Duniho

Raj said:
Even after I remov the 2nd and 3rd Console.Read()s, I don't have any respite!

Whereas, this peace of code is workin fine:
char name=' ';
while (name != '.')
{
name = (Char) Console.Read();
Console.Write(name);
}
Console.Read();

Of course it works fine. Your loop condition is completely different
from the first example.

In your first example, the loop requires a specific character to
continue. In your second example, _any_ character other than a specific
character is required for the loop to continue.

Change your second example to this:

char name='.';

while (name == '.')
{
name = (Char) Console.Read();
Console.Write(name);
}

…and you'll see you get the same behavior you see in your first example.
The loop will only work once.

You got three good answers to your first question, albeit only one that
really pinpointed the problem. It makes one wonder if you actually read
them as carefully as you should have.

Mike's reply should have showed you that your initial question was not
as detailed as it should have been (because he had to guess about what
your actual input was), Gregory provided a great suggestion for
refactoring the loop so that it's easier to maintain (while it doesn't
address the specific input, it would be much easier for you to detect
and fix the problem if your code was structured better), and Anthony
gave not only an explanation of why your loop is seeing something other
than 'Y', but also a suggestion to use an alternative input method so as
to avoid having to deal with the newline characters (in this context, I
think a simple call to ReadKey() would be fine…you don't even need to
check KeyAvailable, since it's fine to block until one is).

I hope this elaboration on the previous three replies helps resolve your
confusion. :)

Pete
 
R

Raj

Thanks indeed for all of your precious time and efforts!

I simply replaced Console.Read with Consoke.KeyChar ... that is working fine
absolutely without changing any condition!

Regards
Raj
 

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

Top