2012-11-14 28 views
18

मेरे पास एक बयान है जो बहुत से चेक अपवाद फेंकता है। मैं इस तरह उन सभी के लिए सभी को पकड़ने ब्लॉक जोड़ सकते हैं:रनटाइम अपवादों को छोड़कर सभी अपवादों को पकड़ना संभव है?

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); 
} 

कि ठीक हो सकता है, सिवाय मैं सभी क्रम अपवाद पकड़ा जा रहा है बिना फेंक दिया जाना चाहते हैं। इस के लिए कोई भी समाधान है? मैं सोच रहा था कि पकड़े जाने के अपवाद के प्रकार की कुछ चतुर सामान्य घोषणा चाल (या शायद नहीं) हो सकती है।

उत्तर

40

आप निम्न कर सकता है:

try { 
    methodThrowingALotOfDifferentExceptions(); 
} catch(RuntimeException ex) { 
    throw ex; 
} catch(Exception ex) { 
    throw new MyCustomInitializationException("Class Resolver could not be initialized.", ex); 
} 
+2

अच्छा उत्तर ... +1 – Juvanis

+2

+1 कूल। मैंने कभी नहीं देखा है लेकिन चाल है। – drasto

11

आप जावा 7 का उपयोग कर सकते हैं, तो आप उपयोग कर सकते हैं एक Multi-Catch:

try { 
    methodThrowingALotOfDifferentExceptions(); 
} catch(IOException|ClassCastException|... ex) { 
    throw new MyCustomInitializationException("Class Resolver could not be initialized.", ex); 
} 
+0

+1 मुझे जावा 7 –

+0

+1 में इस नए मूल्यवान (और लंबे समय से आईएमओ) सुविधा के बारे में चेतावनी दे रहा है +1 इस कारण से मैं जावा 7 का उपयोग कर सकता था। लेकिन मैं इसके लिए बहुत खेद नहीं कर सकता क्योंकि यह मल्टी-कैच सही है। मुझे जावा 6 का उपयोग करना होगा: (( – drasto

1

आप कुछ इस तरह की कोशिश कर सकते मूल रूप से सब कुछ है और फिर पकड़ने के लिए फिर से फेंक RuntimeException अगर यह उस वर्ग का एक उदाहरण है ...

try { 
    methodThrowingALotOfDifferentExceptions(); 
} catch(Exception ex) { 
    if (ex instanceof RuntimeException){ 
     throw ex; 
    } 
    else { 
     throw new MyCustomInitializationException("Class Resolver could not be initialized.", ex); 
    } 
} 

देखकर मानो इस पर लिखने के लिए गन्दा और फिर से (और रख-रखाव के लिए बुरा) हो सकता है, मैं शायद एक अलग वर्ग में कोड को स्थानांतरित करना चाहते हैं, कुछ इस तरह ...

public class CheckException { 
    public static void check(Exception ex, String message) throws Exception{ 
     if (ex instanceof RuntimeException){ 
      throw ex; 
     } 
     else { 
      throw new MyCustomInitializationException(message, ex); 
     } 
    } 
} 

और में इसका इस्तेमाल करते हैं इस तरह अपने कोड ...

try { 
    methodThrowingALotOfDifferentExceptions(); 
} catch(Exception ex) { 
    CheckException.check(ex,"Class Resolver could not be initialized."); 
} 

ध्यान देने योग्य बात है कि हम message में पास इतना है कि हम अभी भी अपनी MyCustomInitializationException अनुकूलित कर सकते हैं।

+0

अंतिम विकल्प अच्छा होगा अगर मुझे इसे कई बार करने की ज़रूरत होती है लेकिन यह मेरा मामला नहीं है - मुझे केवल इसे एक बार उपयोग करने की आवश्यकता है। केवल मैं स्ट्रिंग विधि को 'स्ट्रिंग' 'संदेश। – drasto

+0

आपको कोड में 'ex'' runtimeException' पर डालना होगा। –

संबंधित मुद्दे