2011-08-16 10 views
12

catch()で複数の例外を許可すると、冗長なエラー処理コードの量が減少します。 は例えば、1つのキャッチブロックで複数の異なるタイプの例外処理がありますか?

try{ 
// some statments 
} 
catch(Type1Exception t1, Type2Exception t2, Type3Exception t3) { // wish if this could be allowed 
/* t1, t2, t3 are children of Exception and needs same error handling then why to have different catch blocks with same piece of code */ 
} 
+0

http://download.oracle.com/javase/tutorial/essential/exceptions/index.html – mKorbel

+0

@mKorbel:そして​​...? – mellamokb

+0

@Closer:疑問符が表示されないので、質問を閉じる投票に投票した人は本当に理解できません。さあ、これは面白い質問です! –

答えて

18

はい - それはそれはsupported in Java 7だ理由です。

だからあなたの例では、実際には次のようになります。

try { 
} catch (Type1Exception | Type2Exception | Type3Exception ex) { 
    ... 
} 
+0

'| 'の代わりに' || 'を意味します。 –

+2

@ Eng.Fouad:いいえ、' | 'を意味します。 :-)このチュートリアルでは、「catch節で、ブロックが処理できる例外の種類を指定し、各例外の種類を縦棒(|)で区切っています」 –

+1

@Jon Skeet:.NETに似たものがありますか(前バージョンまたは次バージョン) – VoodooChild

関連する問題