简体   繁体   English

System.ServiceModel.FaultException

[英]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.

相关问题 System.dll中的System.ServiceModel.FaultException - System.ServiceModel.FaultException in System.dll 如何克服System.ServiceModel.FaultException - how to overcome System.ServiceModel.FaultException System.ServiceModel.FaultException - 如何解决这个问题 - System.ServiceModel.FaultException - how to solve this System.ServiceModel.dll中的“ System.ServiceModel.FaultException”:过程不存在 - 'System.ServiceModel.FaultException' in System.ServiceModel.dll: Procedure not present 自承载WCF和System.ServiceModel.FaultException - Self-Hosting WCF and System.ServiceModel.FaultException WCF单元测试结果在System.ServiceModel.FaultException中 - WCF Unit Testing Results in a System.ServiceModel.FaultException 如何解决此错误“ System.ServiceModel.FaultException” - How can i solve this error “System.ServiceModel.FaultException” 在Dynamics CRM中将RetrieveMultiple与FetchXML一起使用时,出现“ System.ServiceModel.FaultException`1” - 'System.ServiceModel.FaultException`1' when using RetrieveMultiple with FetchXML in Dynamics CRM XAMARIN.ANDROID 应用出现 System.ServiceModel.FaultException - XAMARIN ANDROID app got System.ServiceModel.FaultException Synergy dbr.exe在xcall期间获取System.ServiceModel.FaultException - Synergy dbr.exe getting System.ServiceModel.FaultException during xcall
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM