使用故障程式碼丟擲 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
   }
}