4
catch(Exception ex)
{
    //do what you want here

    //When type of exception is System.Web.Services.Protocols.SoapException
    //if (ex.Code.Name.Equals("Client"))
    //{
    //      msg = "service's function not exist";
    //}
    //else if (ex.Code.Name.Equals("Server"))
    //{
    //     msg = "function error"
    //}
    //else
    //{
    //     msg = "unknown";
    //}
    //MessageBox.Show(msg, "error", MessageBoxButtons.OK);

**But ex is not System.Web.Services.Protocols.SoapException so I cannot call ex.Code.Name.Equals("Client")**


//When System.Net.WebException
//switch (ex.Status)
//{
//   case System.Net.WebExceptionStatus.ConnectFailure:
//              do some thing
                break;
//   case System.Net.WebExceptionStatus.Timeout:
                //do some thing
                break;
//    case System.Net.WebExceptionStatus.ProtocolError:
            switch (((System.Net.HttpWebResponse)ex.Response).StatusCode)
            {
                  case System.Net.HttpStatusCode.NotFound:
                        //do some thing
                        break;
                    case System.Net.HttpStatusCode.ServiceUnavailable:
                        //do some thing
                        break;
                    case System.Net.HttpStatusCode.Unauthorized:
                        //do some thing
                        break;
                    default:
                        //do some thing
                        break;
                }
                break;
            default:
                //do some thing
                break;
        }
}

ただし、例外はSystem.Net.WebExceptionではありません。したがって、ex.Statusを呼び出すことはできません

私の問題:

Smartclientソフトウェアには、クライアントとしてWindowsForm、サーバーとしてWebサービスが含まれています。クライアントとサーバーはどちらも私がテストしたn層アプリケーションであり、クライアントからサービスを呼び出すときに問題があることがわかりました

  1. app.config内:サービスのパスが間違っています。System.NotSupportedExceptionをキャッチします
  2. または、サーバーが接続できない場合:System.Net.WebExceptionStatus
  3. サーバーのwebconfigが間違っています:System.InvalidOperationException
  4. サービスは例外をスローします:System.Web.Services.Protocols.SoapException ..。

私のアイデア

私は、他のすべての例外タイプの代表として、representativeAlExceptionである例外と呼びます 。私は名前空間を持っています:Commonと2つのクラスrepresentativeAlException.csBusinessExceptionHandler.cs

paramを(representativeAlException ex)として使用して共通関数を作成します

            try
            {
                Err_LogCheck.Service1.Service1 service = new Err_LogCheck.Service1.Service1();
                return service.getDeviceByZero(ZERO);
            }
            catch (Common.representativeAlException ex)
            {
                Common.BusinessExceptionHandler.ProcessException(ex);
            }

私がやりたいこと

サービスが呼び出される場所。すべてのタイプの例外を処理できるcatchブロックは1つだけです。

ProcessException(representativeAlException ex)関数で

switch (ex)
{
case System.InvalidOperationException:
 //Do some thing
 break;
case System.NotSupportedException:
 //Do some thing
 break;
case System.Web.Services.Protocols.SoapException:
 //do some thing
 break;
...
...
4

4 に答える 4

16

すべての例外を処理するには、Exceptionクラスを使用します。

try
{

}
catch(Exception ex)
{
     switch (ex.GetType().ToString())
     {
         case "System.InvalidOperationException":
              //cast ex to specific type of exception to use it's properties
              ((InvalidOperationException)ex).SomeMethod();
         break;
         case "System.NotSupportedException":
             ((System.NotSupportedException)ex).AnotherMethod();
         break;
         case "System.Web.Services.Protocols.SoapException":
             ((System.Web.Services.Protocols.SoapException)ex).OtherMethod();
         break;
     }

}

とにかく複数のキャッチブロックを使用できないのはなぜですか?

于 2012-10-04T01:06:07.403 に答える
5

User2808350は、try catch ブロックが理由により多くの異なるキャッチを持つことができることは正しいですが、このパターンを使用すると、自分自身 (DRY) を繰り返していることに気付くことがあります。

簡潔で読みやすいコードを生成できる別の方法は、例外が特定の型のインスタンスまたはその型から派生したインスタンスである場合に true と評価されるis演算子を使用することです。残念ながら、switch ブロックでは is演算子を使用できないため、if-then-else を使用する必要があります。

例外の種類をテストし、それに応じて終了コードを設定する例を次に示します。

try
{
    // try something
}
catch (Exception e)
{
    if (e is FileNotFoundException)
        Environment.ExitCode = 2;
    else if (e is InvalidOperationException)
        Environment.ExitCode = 1;
    else
        Environment.ExitCode = 42;
    throw;
}
于 2015-10-23T11:39:12.740 に答える
1

使用する必要があります

try {

    ....

}
catch (System.Net.HttpStatusCode.NotFound)
{
 //do some thing
}    
catch (System.Net.HttpStatusCode.ServiceUnavailable)
{
 //do some thing
}

catch (System.Net.HttpStatusCode.Unauthorized)
{
 //do some thing
}

catch (System.Net.HttpStatusCode.NotFound)
{
 //do some thing
}    
catch (Exception)
{
 //do some thing
}

switch を使ったほうがいい

于 2013-09-23T18:30:02.307 に答える