简体   繁体   English

如何从CompletedEventArgs中获取抛出的异常类型?

[英]How can I get the type of thrown exception from CompletedEventArgs?

I use C# and Visual Studio 2012 to develop a WP8 application. 我使用C#和Visual Studio 2012来开发WP8应用程序。 I added a service reference to my project (Add Service Reference). 我为我的项目添加了一个服务引用(添加服务引用)。 So I am able to use webservice functions. 所以我可以使用webservice功能。

client = new YChatWebService.WebServiceControllerPortTypeClient();

client.getDataCompleted += client_getDataCompleted;
client.InnerChannel.OperationTimeout = new TimeSpan(0, 0, 0, 0, 500);
client.getDataAsync(); 

void client_getDataCompleted(object sender, getDataCompletedEventArgs e)
{
    // e.Error.Message

}

I have set up a timeout limit 500ms for getData() ; 我为getData()设置了500ms的超时限制; If the time limit is exceeded then I get following error: 如果超过时间限制,则会出现以下错误:

"The HTTP request to 'http://example.com/webService/index?ws=1' has exceeded the allotted timeout. The time allotted to this operation may have been a portion of a longer timeout." “对'http://example.com/webService/index?ws=1'的HTTP请求已超出分配的超时时间。分配给此操作的时间可能是较长超时的一部分。”

That is nice :) However, I would like to find out what kind of exception was thrown. 这很好:)但是,我想知道抛出了什么样的异常。 Something like a string variable containing a string " TimeoutException " would be nice. 像包含字符串“ TimeoutException ”的字符串变量之类的东西会很好。 How can I achive that? 我怎么能做到这一点?

You could use 你可以用

if( e.Error is TimeoutException )

To see if the Exception is of the type TimeoutException 查看Exception是否为TimeoutException类型

Isn't this more that the http response from the server contains the error description, not a .net exception? 这不是来自服务器的http响应包含错误描述,而不是.net异常吗?

Does the e.Error class have any other properties other than 'Message'? e.Error类是否具有“Message”以外的任何其他属性? If the server is also .net, then you might be able to match the exception type, but if it's not .net they won't correspond. 如果服务器也是.net,那么您可能能够匹配异常类型,但如果它不是.net则它们将不对应。

If there is an error code returned, I would just use this, or just search for the text 'exceeded the allotted timeout', to confirm that it was a timeout exception. 如果返回了错误代码,我只会使用它,或只搜索“超出分配的超时”文本,以确认它是超时异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何摆脱Visual Studio设计器抛出的“Out of memory”异常? - How can I get rid of “Out of memory” exception thrown from Visual Studio designer? 我如何找到为什么我得到“抛出'System.Web.HttpUnhandledException'类型的异常”? - How do I find why I get "Exception of type 'System.Web.HttpUnhandledException' was thrown"? 我如何知道是否故意抛出了异常? - How can i tell if an Exception was deliberately thrown? 如何确定asp.net Web服务抛出的异常类型? - How can I determine the type of exception thrown by a asp.net web service? 当 HotChocolate GraphQL 服务器中引发异常时,如何获取更多错误详细信息或日志记录? - How can I get more error details or logging, when an exception is thrown in a HotChocolate GraphQL server? 如何在抛出异常时阻止调试器进入我的方法? - How can I prevent the debugger from entering my method when Exception is thrown? 如何从代码中更改“引发异常时中断”调试器行为 - How can I change “Break When an Exception is Thrown” debugger behaviour from code 如何验证使用特定参数引发的异常? - How can I verify an exception was thrown with a certain parameter? 如何吞下catch块中抛出的异常? - How can I swallow an exception that is thrown inside a catch block? 如何检查是否已抛出任何异常? - How can I check if any exception has already been thrown?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM