catch(Exception ex)
{
//do what you want here
//When type of exception is System.Web.Services.Protocols.SoapException
//if (ex.Code.Name.Equals("Client"))
//{
// msg = "service's function not exist";
//}
//else if (ex.Code.Name.Equals("Server"))
//{
// msg = "function error"
//}
//else
//{
// msg = "unknown";
//}
//MessageBox.Show(msg, "error", MessageBoxButtons.OK);
**But ex is not System.Web.Services.Protocols.SoapException so I cannot call ex.Code.Name.Equals("Client")**
//When System.Net.WebException
//switch (ex.Status)
//{
// case System.Net.WebExceptionStatus.ConnectFailure:
// do some thing
break;
// case System.Net.WebExceptionStatus.Timeout:
//do some thing
break;
// case System.Net.WebExceptionStatus.ProtocolError:
switch (((System.Net.HttpWebResponse)ex.Response).StatusCode)
{
case System.Net.HttpStatusCode.NotFound:
//do some thing
break;
case System.Net.HttpStatusCode.ServiceUnavailable:
//do some thing
break;
case System.Net.HttpStatusCode.Unauthorized:
//do some thing
break;
default:
//do some thing
break;
}
break;
default:
//do some thing
break;
}
}
But Exception is not System.Net.WebException. So cannot call ex.Status
My problem:
I have a Smartclient software include WindowsForm as client and a webservice as server. Client and Server both are n-tiers application I have tested and found that has any problem when call service from Client
My Idea
I call the Exception that as representative of all other exception types is representativeAlException I have namespace : Common and two classese representativeAlException.cs and BusinessExceptionHandler.cs
Make a common function with a param as (representativeAlException ex)
try
{
Err_LogCheck.Service1.Service1 service = new Err_LogCheck.Service1.Service1();
return service.getDeviceByZero(ZERO);
}
catch (Common.representativeAlException ex)
{
Common.BusinessExceptionHandler.ProcessException(ex);
}
What I want to do
Where the service is called. Only one catch block can handler for all type of Exception
in ProcessException(representativeAlException ex) function
switch (ex)
{
case System.InvalidOperationException:
//Do some thing
break;
case System.NotSupportedException:
//Do some thing
break;
case System.Web.Services.Protocols.SoapException:
//do some thing
break;
...
...
To handle all exception, use Exception class.
try
{
}
catch(Exception ex)
{
switch (ex.GetType().ToString())
{
case "System.InvalidOperationException":
//cast ex to specific type of exception to use it's properties
((InvalidOperationException)ex).SomeMethod();
break;
case "System.NotSupportedException":
((System.NotSupportedException)ex).AnotherMethod();
break;
case "System.Web.Services.Protocols.SoapException":
((System.Web.Services.Protocols.SoapException)ex).OtherMethod();
break;
}
}
Why can't you just use multiple catch block anyway?
User2808350 is correct that try catch blocks can have many different catches for a reason, however sometimes you find you are repeating yourself (DRY) when using this pattern.
An alternative that can result in concise, readable code is to use the is operator which evaluates to true if your exception is an instance of a particular type or one that derives from that type. Unfortunately it's not possible to use the is operator in a switch block, so you have to use if-then-else.
Here is an example where I test the exception type and set the exit code accordingly.
try
{
// try something
}
catch (Exception e)
{
if (e is FileNotFoundException)
Environment.ExitCode = 2;
else if (e is InvalidOperationException)
Environment.ExitCode = 1;
else
Environment.ExitCode = 42;
throw;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With