我所处的情况是,我需要找到最好的方法来做到这一点,
try
{
// Service call.
}
catch (FaultException exception)
{
service.Abort();
throw new FaultException(Resources.UnexpectedErrorOccurredAtServer, exception);
}或
catch (FaultException exception)
{
service.Abort();
throw new Exception(Resources.UnexpectedErrorOccurredAtServer, exception);
}//主叫方。
Main()
{
try
{
serviceCaller()
}
catch(FaultException ex)
{
// Should we have this catch???
}
catch( Exception ex)
{
// Handle unexpected errors.
}向调用者抛出预期的异常的最佳方法是什么?如果我们抛出FaultException,调用者main方法应该显式处理它,否则一般的异常就会起作用。
发布于 2014-07-24 20:12:19
最好的方法是
try
{
// ...
}
catch (FaultException ex)
{
service.Abort();
throw;
}这将保留堆栈跟踪,而您提出的方法则不会。参见Throwing Exceptions best practices。
发布于 2014-07-24 19:57:37
您可能有兴趣查看WCF error handling and some best practices
最好的方法是这样:
try
{
proxy.SomeOperation();
}
catch (FaultException<MyFaultInfo> ex)
{
// only if a fault contract was specified
}
catch (FaultException ex)
{
// any other faults
}
catch (CommunicationException ex)
{
// any communication errors?
}https://stackoverflow.com/questions/24932382
复制相似问题