当服务器通过webservice引发意外的异常时,抛出哪个.net异常 [英] Which .net exception to throw when the server throws an unexpected exception via webservice
本文介绍了当服务器通过webservice引发意外的异常时,抛出哪个.net异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
哪个.net异常应该抛出客户端(或更好地重新抛出),如果它从服务器捕获任何意外的异常(例如,通过WCF包装为FaultException,服务器可以在java上编写),以进一步处理它客户端如下:
Which .net exception should throw the client (or better re-throw), if it catches any unexpected exception from the server (e.g. wrapped in as FaultException via WCF, the server can be written on java) to handle it further on client as follow:
- 客户端向用户显示有关客户端/服务器中可能发生的错误的消息
<客户端向开发者提供此异常的详细信息(例如每个电子邮件)
业务层中的示例:
var client = new MyWcfClient());
try {
// Try get data.
var data = client.getSomeData(id);
if(data == null) {
// Unexpected exceptions from the server.
// throw new Exception???
}
} catch(FaultException<Specified1Exception> exception) {
// Handle e.g. re-throw.
// Shows the user the myErrorText.
throw new InvalidOperationException(myErrorText)
} catch(FaultException<Specified2Exception> exception) {
// Handle e.g. re-throw.
// Enforce to show the user some dialog.
throw new ArgumentException(param, myText);
} catch(FaultException exception) {
// Unexpected exceptions from the server.
// throw new UnexpectedServerException???
} catch(Exception exception) {
// WCF error, like communication timeout, soap etc.
throw new InvalidOperationException(myOtherErrorText);
}
提供.net框架(本例中为UnexpectedServerException)的任何异常,if不,您会创建哪个异常?
Provides the .net framework any exceptions for (UnexpectedServerException in this example), if not, which exception would you create?
推荐答案
以下示例显示了WCF客户端的常见异常处理:
The following example shows the usual exception handling for WCF clients:
try
{
... // service call
}
catch (FaultException<X> e)
{
// handle fault specified as [FaultContract(typeof(X))]
}
catch (FaultException<Y> e)
{
// handle fault specified as [FaultContract(typeof(Y))]
}
catch (FaultException e)
{
// handle all other faults NOT specified as [FaultContract]
// these are unhandled exception thrown by the service
}
catch (CommunicationException e)
{
// handle all communication-layer exceptions
// these are exceptions thrown by the WCF infrastucture
}
这篇关于当服务器通过webservice引发意外的异常时,抛出哪个.net异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文