473,511 Members | 14,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

error detection

cj
I'm working on error handling in my program and need some input.

Dim errorsFound As Boolean = False
Dim respstr As String
Try
respstr = proxy.validate(soapmesg)
Catch ex As Exception
errorLabel.Text = ex.Message
errorsFound = True
End Try
If Not errorsFound .....

I'd rather not use boolean errorsFound. What I don't know is if an
exception occured in respstr=proxy.validate(soapmesg) can I count on
respstr being empty? If so I'll just say If not respstr = ""
Mar 16 '06 #1
13 2107
cj wrote:
I'm working on error handling in my program and need some input.

Dim errorsFound As Boolean = False
Dim respstr As String
Try
respstr = proxy.validate(soapmesg)
Catch ex As Exception
errorLabel.Text = ex.Message
errorsFound = True
End Try
If Not errorsFound .....

I'd rather not use boolean errorsFound. What I don't know is if an
exception occured in respstr=proxy.validate(soapmesg) can I count on
respstr being empty? If so I'll just say If not respstr = ""


Unless you wrote or have documenation to what returns during an error
you should not count on respstr being anything. I would reset it any
time there is an error.

Try
respstr = proxy.validate(soapmesg)
Catch ex As Exception
errorLabel.Text = ex.Message
respstr = string.empty
End Try

Another thought would be to leave the funtion if there is an error.
Mar 16 '06 #2
Chris wrote:
Unless you wrote or have documenation to what returns during an error
you should not count on respstr being anything. I would reset it any
time there is an error.

Try
respstr = proxy.validate(soapmesg)
Catch ex As Exception
errorLabel.Text = ex.Message
respstr = string.empty
End Try


Or initialize respstr outside of the try block:

respstr = string.empty
Try
respstr = proxy.validate(soapmesg)
Catch ex As Exception
errorLabel.Text = ex.Message
End Try

You could then use it (and therefore test if it is empty) outside of the try
block.
Mar 16 '06 #3
cj
Oh Oh, in my haste to make an example I forgot to say Dim respstr As
String = ""

Sorry

The point to the question is is could respstr=proxy.validate(soapmesg)
set respstr to something if it generates an exception?

You're indicating it would not. I tend to agree. But Chris said it
might. I don't know how firm each of you are in your beliefs. Let's
see what some others have to say. And by all means feel free to reply
again yourself.
Leon Mayne wrote:
Chris wrote:
Unless you wrote or have documenation to what returns during an error
you should not count on respstr being anything. I would reset it any
time there is an error.

Try
respstr = proxy.validate(soapmesg)
Catch ex As Exception
errorLabel.Text = ex.Message
respstr = string.empty
End Try


Or initialize respstr outside of the try block:

respstr = string.empty
Try
respstr = proxy.validate(soapmesg)
Catch ex As Exception
errorLabel.Text = ex.Message
End Try

You could then use it (and therefore test if it is empty) outside of the try
block.

Mar 16 '06 #4
cj
Good idea. Are you sure it's necessary? Leon seems to think that if
that line throws and exception it would not have changed the value of
respstr. Take a look at my response to his message. Thanks!

Chris wrote:
cj wrote:
I'm working on error handling in my program and need some input.

Dim errorsFound As Boolean = False
Dim respstr As String
Try
respstr = proxy.validate(soapmesg)
Catch ex As Exception
errorLabel.Text = ex.Message
errorsFound = True
End Try
If Not errorsFound .....

I'd rather not use boolean errorsFound. What I don't know is if an
exception occured in respstr=proxy.validate(soapmesg) can I count on
respstr being empty? If so I'll just say If not respstr = ""


Unless you wrote or have documenation to what returns during an error
you should not count on respstr being anything. I would reset it any
time there is an error.

Try
respstr = proxy.validate(soapmesg)
Catch ex As Exception
errorLabel.Text = ex.Message
respstr = string.empty
End Try

Another thought would be to leave the funtion if there is an error.

Mar 16 '06 #5
cj wrote:
Oh Oh, in my haste to make an example I forgot to say Dim respstr As
String = ""

Sorry

The point to the question is is could respstr=proxy.validate(soapmesg)
set respstr to something if it generates an exception?

You're indicating it would not. I tend to agree. But Chris said it
might. I don't know how firm each of you are in your beliefs. Let's
see what some others have to say. And by all means feel free to reply
again yourself.
Leon Mayne wrote:
Chris wrote:
Unless you wrote or have documenation to what returns during an error
you should not count on respstr being anything. I would reset it any
time there is an error.

Try
respstr = proxy.validate(soapmesg)
Catch ex As Exception
errorLabel.Text = ex.Message
respstr = string.empty
End Try


Or initialize respstr outside of the try block:

respstr = string.empty
Try
respstr = proxy.validate(soapmesg)
Catch ex As Exception
errorLabel.Text = ex.Message
End Try

You could then use it (and therefore test if it is empty) outside of
the try block.

I'm not sure if it will or not, but my theory is, why should I risk it?

Chris
Mar 17 '06 #6
Hi,

If the exception occured when we call the proxy.validate(soapmesg), then
respstr will be untouched.

Also if you do not continue the process in the function, why not just
return in the catch.
If you have finally code, just put it in the Finally block.

You may try to run the code below.

Try
Throw New Exception("Test")
Catch ex As Exception
MsgBox(ex.ToString())
Return
Finally
MsgBox("Finally called")
End Try

If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Mar 17 '06 #7
cj
I can understand your theory but I feel there shouldn't be any guessing
here. There should be an answer. And we should be able to find it.
It's a programming language and we should be able to know what it's
going to do.
I Don't Like Spam wrote:
cj wrote:
Oh Oh, in my haste to make an example I forgot to say Dim respstr As
String = ""

Sorry

The point to the question is is could respstr=proxy.validate(soapmesg)
set respstr to something if it generates an exception?

You're indicating it would not. I tend to agree. But Chris said it
might. I don't know how firm each of you are in your beliefs. Let's
see what some others have to say. And by all means feel free to reply
again yourself.
Leon Mayne wrote:
Chris wrote:
Unless you wrote or have documenation to what returns during an error
you should not count on respstr being anything. I would reset it any
time there is an error.

Try
respstr = proxy.validate(soapmesg)
Catch ex As Exception
errorLabel.Text = ex.Message
respstr = string.empty
End Try

Or initialize respstr outside of the try block:

respstr = string.empty
Try
respstr = proxy.validate(soapmesg)
Catch ex As Exception
errorLabel.Text = ex.Message
End Try

You could then use it (and therefore test if it is empty) outside of
the try block.

I'm not sure if it will or not, but my theory is, why should I risk it?

Chris

Mar 17 '06 #8
cj
I agree it shouldn't be touched. I feel a lot more confident now that
it will not be.

I feel some folks might have been confused on what I was asking. I see
two types of "errors". I never had a problem with an error being
returned by validate as it'd be returned in respstr and will be checked
for. That type of error wouldn't be an exception caught in ex. This
question was strictly for if the statement throws an exception. Suppose
the connection to the remote machine was lost and validate couldn't be
run. I expect it'd throw an exception and I was confirming respstr in
such a situation would be untouched.

Peter Huang [MSFT] wrote:
Hi,

If the exception occured when we call the proxy.validate(soapmesg), then
respstr will be untouched.

Also if you do not continue the process in the function, why not just
return in the catch.
If you have finally code, just put it in the Finally block.

You may try to run the code below.

Try
Throw New Exception("Test")
Catch ex As Exception
MsgBox(ex.ToString())
Return
Finally
MsgBox("Finally called")
End Try

If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Mar 17 '06 #9
cj
As per your inquiry about why I don't return.

This statement is inside a do loop. If it has an exception I want to
restart the do loop with the next iteration immediately and not run the
rest of the code in the loop--or skip the rest of the code in the loop.
If you'd seen my post on "I miss loop" from 3/14 you'll know that this
was a sticky situation. If I exit do it LEAVES the loop. I no longer
have the command to initiate an immediate jump to the next iteration.

In this section of code if an exception occurs I display it in labels on
the form in case someone is looking but there is little that can be done
about it by the program except try the next loop iteration. So in this
case I really only catch exceptions to keep them from messing up my
execution. The code following the command is looking to see what's in
respstr already. I didn't want to have to set another variable to
indicate an exception occured so respstr can't be trusted. Of coure as
has been suggested, and is a good idea I could ensure respstr is blank
in the catch. But why add an extra line. I know respstr is empty
before the command is run and I SHOULD know if throwing an exception
would put put anything in it.

Peter Huang [MSFT] wrote:
Hi,

If the exception occured when we call the proxy.validate(soapmesg), then
respstr will be untouched.

Also if you do not continue the process in the function, why not just
return in the catch.
If you have finally code, just put it in the Finally block.

You may try to run the code below.

Try
Throw New Exception("Test")
Catch ex As Exception
MsgBox(ex.ToString())
Return
Finally
MsgBox("Finally called")
End Try

If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Mar 17 '06 #10
cj wrote:
Good idea. Are you sure it's necessary? Leon seems to think that if
that line throws and exception it would not have changed the value of
respstr. Take a look at my response to his message. Thanks!


Ah right, I see what you're saying now. In theory respstr should be empty if
the function call threw an exception, as there would not have been a return
value. So you should be able to rely on respstr being empty, yes.
Mar 18 '06 #11
Hi

I think you may take a look at the code below for your reference.

I can not test based on all the possibility, but commonly if the
request.GetResponse(); throw exception, the response will be null;

static void Main(string[] args)
{

// Create a request for the URL.
WebRequest request = WebRequest.Create(
"http://localhost");
// If required by the server, set the credentials.
// request.Credentials = CredentialCache.DefaultCredentials;
WebResponse response = null;
try
{
// Get the response.
response = request.GetResponse();
// Display the status.
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
if (response != null)
{

Console.WriteLine(((HttpWebResponse)response).Stat usDescription);
// Get the stream containing content returned by the
server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams and the response.
reader.Close();
response.Close();
}

}

Also from VB.NET 2005, we have a continue keyword, which can be used to go
the next iteration of the loop without go through the all the following
code.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Mar 20 '06 #12
cj
Thanks

Peter Huang [MSFT] wrote:
Hi

I think you may take a look at the code below for your reference.

I can not test based on all the possibility, but commonly if the
request.GetResponse(); throw exception, the response will be null;

static void Main(string[] args)
{

// Create a request for the URL.
WebRequest request = WebRequest.Create(
"http://localhost");
// If required by the server, set the credentials.
// request.Credentials = CredentialCache.DefaultCredentials;
WebResponse response = null;
try
{
// Get the response.
response = request.GetResponse();
// Display the status.
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
if (response != null)
{

Console.WriteLine(((HttpWebResponse)response).Stat usDescription);
// Get the stream containing content returned by the
server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams and the response.
reader.Close();
response.Close();
}

}

Also from VB.NET 2005, we have a continue keyword, which can be used to go
the next iteration of the loop without go through the all the following
code.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Mar 20 '06 #13
cj
Thanks.

Leon Mayne wrote:
cj wrote:
Good idea. Are you sure it's necessary? Leon seems to think that if
that line throws and exception it would not have changed the value of
respstr. Take a look at my response to his message. Thanks!


Ah right, I see what you're saying now. In theory respstr should be empty if
the function call threw an exception, as there would not have been a return
value. So you should be able to rely on respstr being empty, yes.

Mar 20 '06 #14

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

Similar topics

0
3314
by: Cherrish Vaidiyan | last post by:
sir, The following are the steps that i followed in setting up standby database on Red hat Linux 9. i am using Oracle 9i. i have followed the steps in this site : ...
2
2996
by: andy johnson | last post by:
I made the mistake of "upgrading" to IE6.0 on my windoze 98 laptop. I really wish bill gates would die in a horribly painful accident. Anyway, after much grief, I downloaded Opera and saw true...
2
2086
by: Simon | last post by:
I have recently set up a server certificate on a web site. Under certain conditions I need to change the color of a html span element. I do this using the following javascript function called from...
3
11443
by: news.onetel.net.uk | last post by:
I and my friend Karl have spent literally all day trying to find out what is causing my error but we are zapped of any further functionality :) I have a form that adds news records. You select...
7
5866
by: debugger | last post by:
hello, Question, on page load, I populate an existing drop down with createElement and appendChild. It works fine so far. BUT I want to automatically select some option from this populated drop...
35
3738
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
0
1759
by: JohnQ | last post by:
(The thread "Error Handling Idioms" prompted this post. I meant to post it at the top level, but it got posted as a reply. So here it is again!) An attempt at common defintions: fault: the...
2
1776
by: coolsmaster | last post by:
When I put my code through different inputs, one form of input results in a problem. when I enter: updatename 123456789 10 f9 the expected output is "Error: 10 is out of the range 0-4" ...
3
7313
by: harshadanarvekar | last post by:
Hi Everyone, Here is a part of javascript code that works well in FF2 but shows above error in IE6 and error "missing name after . operator" in NS8 function PopUp(idf,stepX,stepY,speed){
0
1898
by: origami.takarana | last post by:
Intrusion Detection Strategies ----------------------------------- Until now, we’ve primarily discussed monitoring in how it relates to intrusion detection, but there’s more to an overall...
0
7252
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5676
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5077
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.