ArgumentException
-
Hi I have a web method (see below) which accepts an integer as a parameter. I want to be able to catch the error if a paramter is not provided. I thought using catch (ArgumentException) would do the job Is there anything else i should be doing to be able to catch errors like this ? thanks Simon
\[WebMethod\] public DataSet GetOrganisation(int organisationId) { DataSet ds = new DataSet(); try {
.....
} catch (MySql.Data.MySqlClient.MySqlException ex) { WriteToLog(organisationId.ToString(), "", "GetOrganisation", ex.Message); } catch (ArgumentException argerr) { WriteToLog(organisationId.ToString(), "", "GetOrganisation", argerr.Message); } return ds; }
-
Hi I have a web method (see below) which accepts an integer as a parameter. I want to be able to catch the error if a paramter is not provided. I thought using catch (ArgumentException) would do the job Is there anything else i should be doing to be able to catch errors like this ? thanks Simon
\[WebMethod\] public DataSet GetOrganisation(int organisationId) { DataSet ds = new DataSet(); try {
.....
} catch (MySql.Data.MySqlClient.MySqlException ex) { WriteToLog(organisationId.ToString(), "", "GetOrganisation", ex.Message); } catch (ArgumentException argerr) { WriteToLog(organisationId.ToString(), "", "GetOrganisation", argerr.Message); } return ds; }
huh? you can't pass anything BUT an int to that method. Its not an optional parameter.
-
Hi I have a web method (see below) which accepts an integer as a parameter. I want to be able to catch the error if a paramter is not provided. I thought using catch (ArgumentException) would do the job Is there anything else i should be doing to be able to catch errors like this ? thanks Simon
\[WebMethod\] public DataSet GetOrganisation(int organisationId) { DataSet ds = new DataSet(); try {
.....
} catch (MySql.Data.MySqlClient.MySqlException ex) { WriteToLog(organisationId.ToString(), "", "GetOrganisation", ex.Message); } catch (ArgumentException argerr) { WriteToLog(organisationId.ToString(), "", "GetOrganisation", argerr.Message); } return ds; }
As long as you pass an integer, any integer, to this method no exception will be generated. What you can do is check for a valid range then throw an exception if the variable passed to this method doesn't fall within that range. This can be done in several ways, hard coded, using Validation Application Block or Code Contracts. It's up to you.
No comment
-
huh? you can't pass anything BUT an int to that method. Its not an optional parameter.
Notice that it's a web method. If somebody tries to call the web method via a web service, an exception will be thrown, but it can't be caught in that method. There may be another place to catch such errors on the server-side (before the client gets an exception), but I'm not sure.
Somebody in an online forum wrote:
INTJs never really joke. They make a point. The joke is just a gift wrapper.
-
Notice that it's a web method. If somebody tries to call the web method via a web service, an exception will be thrown, but it can't be caught in that method. There may be another place to catch such errors on the server-side (before the client gets an exception), but I'm not sure.
Somebody in an online forum wrote:
INTJs never really joke. They make a point. The joke is just a gift wrapper.
You mean if somebody builds their own raw XML and calls the method directly? If you use a generated proxy class and its "out of date", the proxy class will handle the exception. Not quite sure what you are trying to accomplish.
-
You mean if somebody builds their own raw XML and calls the method directly? If you use a generated proxy class and its "out of date", the proxy class will handle the exception. Not quite sure what you are trying to accomplish.
I'm not trying to accomplish anything. I was just clarifying what the OP seems to be trying to get at. Seems like they want to have control over what is returned in the case that a bad request is received (e.g., return a message that details which parameters were missing from the call).
Somebody in an online forum wrote:
INTJs never really joke. They make a point. The joke is just a gift wrapper.
-
Hi I have a web method (see below) which accepts an integer as a parameter. I want to be able to catch the error if a paramter is not provided. I thought using catch (ArgumentException) would do the job Is there anything else i should be doing to be able to catch errors like this ? thanks Simon
\[WebMethod\] public DataSet GetOrganisation(int organisationId) { DataSet ds = new DataSet(); try {
.....
} catch (MySql.Data.MySqlClient.MySqlException ex) { WriteToLog(organisationId.ToString(), "", "GetOrganisation", ex.Message); } catch (ArgumentException argerr) { WriteToLog(organisationId.ToString(), "", "GetOrganisation", argerr.Message); } return ds; }
Normally, in .NET I create wrapper classes for all of my web methods.
[WebMethod]
public FooBarResult FooBar(FooBarParameters parameters){ //FooBarParameters only if a lot
FooBarResult result = new FooBarResult();
try{
... do work here
}
catch(Exception e1){
... Perform logic to determine how much of the exception you can return and
... assign that to the result object in a special errors property
}
//Sometimes I will add misc. information like run time to the result here
}While not perfect in any manner it lets me consistently write web methods and always know how the results are coming back. I do a lot of cross platform WS work which is a real pain because both the Java tools and the .NET tool behave differently. This pattern gives a small semblance of sanity.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
Hi I have a web method (see below) which accepts an integer as a parameter. I want to be able to catch the error if a paramter is not provided. I thought using catch (ArgumentException) would do the job Is there anything else i should be doing to be able to catch errors like this ? thanks Simon
\[WebMethod\] public DataSet GetOrganisation(int organisationId) { DataSet ds = new DataSet(); try {
.....
} catch (MySql.Data.MySqlClient.MySqlException ex) { WriteToLog(organisationId.ToString(), "", "GetOrganisation", ex.Message); } catch (ArgumentException argerr) { WriteToLog(organisationId.ToString(), "", "GetOrganisation", argerr.Message); } return ds; }
Question doesn't make any sense. GetOrganisation() will not be invoke unless there is a value for organisationId. So there is no way to test in that method whether a client called it with a value or not. If you are asking about client code that calls that method then a much more likely error scenario is a communication fault. Although it is possible to call the server such that it is unable to resolve the method that is unlikely to be a valid scenario and it isn't a scenario that will exist for long. And even then it is a problem with the client code.