使用故障代码抛出 FaultException

FaultException 还可以包含 FaultCode ,它是可用于传递其他信息的字符串数据,因此客户端可以区分不同的异常:

try
{
    // your service logic here
}
catch (Exception ex)
{
   throw new FaultException("There was a problem processing your request",
      new FaultCode(("01"));
}

获取 FaultCode:

try
{
    // call the service
}
catch (FaultException faultEx)
{
   switch (faultEx.Code.Name)
   {
    case "01":
       // do something
       break;
    case "02":
        // do another something
        break
   }
}