0

私の現在の「水域をテストする」プロジェクトでは、Try-Catch ブロックを使用せず、代わりに (致命的以外の) 各エラーを他の方法でキャッチしようとしています。

ここで、エラーをキャッチすると言うと、私の非常に不自然なプログラムは 1 つのエラーを作成しますが、これは簡単に回避できます。除算を試み0ますが、これは If ステートメントで防ぐことができます。簡単にするために、1 つのクラスと 2 つのメソッドを含む 1 つの C# ファイルしかありません。これは、コンストラクターがプロセスを開始するテンプレートのようなものだと思います。

public class myObject
{
    public myObject()
    {
       Object objOne = methodOne();
       methodThree(objOne);
    }

    public object methodOne()
    {
        //logic to create a return object

        int x = 0;  

        //I've added a condition to ensure the maths is possible to avoid raising an exception when, for this example, it fails
        if (x > 0)
            int y = 5 / x;

        return object;
    }

    public void procesObjects(Object objOne)
    { 
        //logic  
    }    
}

そのため、ご覧のとおり、数学が で除算されていないことを確認するステートメントをmethodOne()追加しました。しかし、私はそれをキャッチしたので、望ましくないアプリケーションが続行されます。アプリケーションを停止し、デバッグのために失敗をログに記録する方法が必要です。if0

だから、これは私がうまくいくと思うものです:

この例では、非常に単純なクラスTrackingを作成します (または構造体の方がよいでしょうか?)。

public class Tracking
    {
        StringBuilder logMessage = new StringBuilder();
        bool readonly hasFailed;
    }

その後、コードを次のように更新できます。

public class myObject
{
    Tracking tracking = new Tracking();
    public myObject()
    {
       Object objOne = methodOne();
       if (!tracking.hasFailed)
           methodThree(objOne);
       if (tracking.hasFailed)
           ExteranlCallToLog(tracking);
    }
    public object methodOne()
    {
        //logic
        int x = 0;  
        //I've added a condition to ensure the maths is possible to avoid raising an exception when, for this example, it fails
        if (x > 0)
            int y = 5 / x;
        else
        {
            tracking.hasFailed = true;
            tracking.logMessage.AppendLine("Cannot divide by 0");
        }
         //may also need to check that the object is OK to return
        return object;
    }
    public void procesObjects(Object objOne)
    { 
        //logic  
    }    
}

それで、私が達成しようとしていることを理解していただければ幸いですが、3 つの質問があります。

  1. 私の追跡オブジェクト (この例のように) は、クラスまたは構造体である必要がありますか?
  2. 私のコードが非常にうるさくなるのではないかと心配しています。システムに障害が発生した場合、Trackingオブジェクト内でログに記録してから何らかの形でプログラムを閉じるイベントが発生するのではないかと思いますか?
  3. 他のアイデアは大歓迎です。

繰り返しますが、Try-Catch ブロックを使用する方がシンプルで簡単な場合があることを理解していますが、私自身の教育のために意図的にそれらを避けようとしています。

編集

上記の理由は、次のブログを読んだことが原因でした: Vexing exceptions - Fabulous Adventures In Coding - Site Home - MSDN Blogs

4

1 に答える 1

4

Seriously, Dave - try catch blocks are there for a reason. Use them.

Reading between the lines, it looks like you want to track custom information when something goes wrong. Have you considered extending System.Exception to create your own bespoke implementation suited to your needs?

Something along the lines of:-

public class TrackingException : System.Exception 
{
   // put custom properties here.
}

That way, when you detect that something has gone wrong, you can still use try/catch handling, but throw an exception that contains pertinent information for your needs.

于 2012-11-01T11:16:55.423 に答える