[英]System.ServiceModel.FaultException<DefaultFaultContract not catching some exceptions
I am having a code function that has two catch blocks. 我有一个具有两个catch块的代码功能。 Am posting the code below:
正在发布以下代码:
public void UpdateGroup(String strSiteID, String strGroup, int row)
{
try
{
Console.WriteLine("UpdateGroup");
Excel1.MWMClient.MWMServiceProxy.Group group = new Excel1.MWMClient.MWMServiceProxy.Group();
group.name = "plumber";
group.description = "he is a plumber";
Console.WriteLine(groupClient.UpdateGroup(strSiteID, group));
ExcelRecorder(0, null, null, row);
}
catch (System.ServiceModel.FaultException<DefaultFaultContract> ex)
{
ExcelRecorder(ex.Detail.ErrorCode, ex.Detail.Message, ex.Message, row);
}
catch (Exception ex)
{
ExcelRecorder(0, "", ex.Message, row);
}
finally
{
System.GC.Collect();
}
}
I thought that the first catch block was enough to catch all the possible exceptions that can occur in my code. 我认为第一个catch块足以捕获代码中可能发生的所有可能的异常。 But I notice that, at times, the first catch block is not catching some general exceptions.
但是我注意到,有时第一个catch块没有捕获一些一般异常。 That is why I added second catch block.
这就是为什么我添加了第二个catch块的原因。 Why is it happening?
为什么会这样呢? Why can't my first catch block cover all exceptions?
为什么我的第一个catch块不能涵盖所有异常?
Why is it happening?
为什么会这样呢? Why can't my first catch block cover all exceptions?
为什么我的第一个catch块不能涵盖所有异常?
Because by default, when there's no any fault contracts, defined for particular service operation, non-generic FaultException
will be thrown at service side (and it will be caught at client side). 因为默认情况下,当没有针对特定服务操作定义的任何故障合同时,非通用的
FaultException
将在服务端引发(并且将在客户端捕获)。
The Exception class System.ServiceModel.FaultException
can only handle contractually specified Faults Exception类
System.ServiceModel.FaultException
只能处理合同规定的Faults
You should be having a statement similar to the one below in your code 您应该在代码中使用与以下语句相似的语句
throw new FaultException<DefaultFaultContract>(<parmaters>);
which is causing the exception to be handled by the first catch block. 这导致异常由第一个catch块处理。 When you don't have a operation fault contractually specified,it will be handled by the generic Exception class(second catch block),from which all exception types derive.
如果未按合同规定指定操作错误,则将由通用Exception类(第二个catch块)处理该异常,所有异常类型都将从该异常类派生。
Please do check for the code samples given on this page. 请检查此页面上提供的代码示例。 http://msdn.microsoft.com/en-us/library/ms576199.aspx
http://msdn.microsoft.com/en-us/library/ms576199.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.