多くのチェックされた例外をスローするステートメントがあります。次のように、すべてのキャッチブロックを追加できます。
try {
methodThrowingALotOfDifferentExceptions();
} catch(IOException ex) {
throw new MyCustomInitializationException("Class Resolver could not be initialized.", ex);
} catch(ClassCastException ex) {
throw new MyCustomInitializationException("Class Resolver could not be initialized.", ex);
} catch...
これらはすべて同じように処理されるため、コードの重複があり、作成するコードも多いため、これは好きではありません。代わりにキャッチすることができException
ます:
try {
methodThrowingALotOfDifferentExceptions();
} catch(Exception ex) {
throw new MyCustomInitializationException("Class Resolver could not be initialized.", ex);
}
すべてのランタイム例外をキャッチせずに破棄したい場合を除いて、それは問題ありません。これに対する解決策はありますか?キャッチされる例外のタイプの巧妙な一般的な宣言がうまくいくかもしれない(あるいはそうでないかもしれない)と私は考えていました。