2

これには必ずしも「正しい」答えがあるとは限らないと思います。おそらくそれはスタイルの問題ですが、try/catch ブロックをどのように構築するのか疑問に思うことがよくあります。

たとえば、以下の架空のコードで概説されている 2 つのメソッド (純粋に説明用) を取り上げます。複数回呼び出す例外をスローするメソッドがありますが、どの呼び出しかによって異なる処理が必要になります。同様に、さまざまなハンドラーでさまざまな種類の例外がスローされる可能性があります。

private Object exceptionMethod() throws Exception {
    throw new Exception("Something bad happened");
}

public void useMethodSequentialHandlers() {
    Object o1; // Must be declared in a wider scope than where it is used
    try {
        o1 = exceptionMethod();
    } catch (Exception ex) {
        // Assume we cannot continue after this exception so we'll return or
        // rethrow to exit the method
        return;
    }

    Object o2; // Must be declared in a wider scope than where it is used
    // Do something that requires o1
    for (int i = 0; i < 100; i++) {
        try {
            o2 = exceptionMethod();
            // Here we would use the objects in some manner
            boolean equal = o1.equals(o2);// Just a pointless example
                                                // to show that both objects
                                                // are required
                // Assume the method does a load of stuff down here
        } catch (Exception ex) {
            // Assume we can continue to the next iteration after this exception
            continue;
        }
    }
}

私が見ているように、try/catch ブロックを順番に並べることの利点は、例外に応答している正確な時点が読者にとってより明確であるため、おそらくコードがより明確になることです。欠点は、メソッドのさまざまな場所に例外処理が散らばっていることと、必要以上に広いスコープで宣言された変数があることです (これは悪いことですか?)。

または:

public void useMethodNestedHandlers() {
    try {
        Object o1 = exceptionMethod(); // Can be declared inside scope where it is used
        // Do something that requires o1
        for (int i = 0; i < 100; i++) {
            try {
                Object o2 = exceptionMethod(); // Can be declared inside scope where it is used
                // Here we would use the objects in some manner
                boolean equal = o1.equals(o2); // Just a pointless example
                                                // to show that both objects
                                                // are required
                // Assume the method does a load of stuff down here
            } catch (Exception ex) {
                // Assume we can continue to the next iteration after this
                // exception
                continue;
            }
        }
    } catch (Exception ex) {
        // Assume we cannot continue after this exception so we'll return or
        // rethrow to exit the method
        return;
    }
}

ここでは、例外処理ロジックをまとめて保持し、変数は使用されるスコープ内で宣言されます。ただし、例外処理ロジックは、発生点から離れているため、あまり明確ではないようです。どちらが良いかについて意見を持っている人はいますか、それとも私は無意味な細かな点について心配しているだけで、仕事を続けるべきですか? :-)

ありがとう

4

4 に答える 4

1

すべてのケースで、答えがテクニカル分析から明確に得られない場合は、最初の開発作業を無視して、コードの将来を研究する必要があると私は信じています。

この目的のために、2 番目の方法を選択する実際の技術的な理由がない限り、最初の方法を最良の選択としてお勧めします。

要約すれば:

2 つのスタイルに技術的な違いがない場合は、コードの将来の読者を考慮して、可能な限り明確にします

于 2012-10-11T12:12:14.037 に答える
1

例外の優れた点は、発生した場所で例外を処理する必要がないことです。そのため、実際には 2 番目のスタイルを使用する必要がありますが、外側の try-catch は使用しません。

public void useMethodNestedHandlers() {
    Object o1 = exceptionMethod(); // Can be declared inside scope where it is used
    // Do something that requires o1
    for (int i = 0; i < 100; i++) {
        try {
            Object o2 = exceptionMethod(); // Can be declared inside scope where it is used
            // Here we would use the objects in some manner
            boolean equal = o1.equals(o2); // Just a pointless example
                                            // to show that both objects
                                            // are required
            // Assume the method does a load of stuff down here
        } catch (Exception ex) {
            // Assume we can continue to the next iteration after this
            // exception
            continue;
        }
    }
}

ハッピー デイ シナリオのコードを作成し、他の誰かに障害の心配を任せます。これが問題の分離を実現する方法です。通常、すべての障害は同じコード片で処理され、早期にキャッチすると重複したコードが発生します。

于 2012-10-11T12:15:44.667 に答える
0

コードは1)正しく、2)読み取​​り可能である必要があります。通常、回復不能な例外はすべて、アプリケーションの最上位層で処理する必要があります(またはまったく処理しないでください)。つまり、ユーザーに適切に表示する必要があります。回復可能なすべての例外は、可能な限り「高い」処理を行う必要があります。できるだけ少ないtry-catchステートメントを使用することをお勧めします

于 2012-10-20T23:22:56.607 に答える
0

状態にもよりますがどちらも良いと思います。

ケース1

  Object obj;
   try {
     // do something 
  } catch (Exception e) {
        obj = default_obj; // assign default object
  }

   try {
      // do something either with specific or default object
   } catch (Exception e) {
       // handle exception
   }

ここで最初の try catch が失敗しても、デフォルト値でアクションを続行します

ケース 2

try {
    Object obj; 
     // acquire object

    // do something only if acquire object is successful
} catch (Exception e) {
   // handle exception
}

オブジェクトの取得に失敗した場合は、ここから先に進まないでください。

ここでは、例外を処理する方法がスタイルよりも必要です。

于 2012-10-11T12:04:20.903 に答える