2010-07-30 15 views
13

.NET में एग्रीगेट एक्सेप्शन क्लास आपको अपवाद फेंकने की अनुमति देता है जिसमें कई अपवाद हैं।.NET से AggregateException के जावा समतुल्य क्या है?

उदाहरण के लिए, यदि आप समांतर में कई कार्यों को चलाते हैं तो आप एक समेकित अपवाद फेंकना चाहते हैं और उनमें से कुछ अपवादों के साथ विफल रहे हैं।

क्या जावा के बराबर वर्ग है?

विशिष्ट मामले मैं में उपयोग करना चाहते हैं:

public static void runMultipleThenJoin(Runnable... jobs) { 
    final List<Exception> errors = new Vector<Exception>(); 
    try { 
     //create exception-handling thread jobs for each job 
     List<Thread> threads = new ArrayList<Thread>(); 
     for (final Runnable job : jobs) 
      threads.add(new Thread(new Runnable() {public void run() { 
       try { 
        job.run(); 
       } catch (Exception ex) { 
        errors.add(ex); 
       } 
      }})); 

     //start all 
     for (Thread t : threads) 
      t.start(); 

     //join all 
     for (Thread t : threads) 
      t.join();    
    } catch (InterruptedException ex) { 
     //no way to recover from this situation 
     throw new RuntimeException(ex); 
    } 

    if (errors.size() > 0) 
     throw new AggregateException(errors); 
} 
+0

मैं एक के बारे में पता नहीं कर रहा हूँ के लिए बनाया गया था कुछ इसी तरह करना होगा। हालांकि, मैंने कभी भी एक की तलाश नहीं की है। – Powerlord

उत्तर

3

मुझे किसी अंतर्निहित या लाइब्रेरी कक्षाओं के बारे में पता नहीं है, क्योंकि मैंने कभी ऐसा करने की इच्छा रखने के बावजूद कभी नहीं किया है (आमतौर पर आप केवल अपवादों को चेन करेंगे), लेकिन यह मुश्किल नहीं होगा खुद को लिखें

आप शायद "प्राथमिक" होने के लिए तो यह stacktraces में भरने के लिए इस्तेमाल किया जा सकता अपवाद में से एक लेने के लिए, आदि चाहते हैं

public class AggregateException extends Exception { 

    private final Exception[] secondaryExceptions; 

    public AggregateException(String message, Exception primary, Exception... others) { 
     super(message, primary); 
     this.secondaryExceptions = others == null ? new Exception[0] : others; 
    } 

    public Throwable[] getAllExceptions() { 

     int start = 0; 
     int size = secondaryExceptions.length; 
     final Throwable primary = getCause(); 
     if (primary != null) { 
      start = 1; 
      size++; 
     } 

     Throwable[] all = new Exception[size]; 

     if (primary != null) { 
      all[0] = primary; 
     } 

     Arrays.fill(all, start, all.length, secondaryExceptions); 
     return all; 
    } 

} 
0

मैं वास्तव में नहीं दिख रहा है यही कारण है कि आप पहली जगह में अपवाद के रूप में इस्तेमाल करना चाहिए अधूरा/विफल रहा है, लेकिन किसी भी मामले में, यह shouldn कार्यों को चिह्नित करने के खुद को बनाने के लिए मुश्किल नहीं है। साझा करने के लिए कोई कोड मिला ताकि हम आपको एक और विशिष्ट उत्तर के साथ मदद कर सकें?

+0

मैं इसे किसी भी चीज़ को 'चिह्नित' करने के लिए उपयोग नहीं कर रहा हूं, मैं बस कम-से-कम-विफलता को इंगित करना चाहता हूं। मैंने कोड को शामिल करने के लिए मुख्य पोस्ट संपादित किया है। –

+0

एक उदाहरण यह उपयोगी है सत्यापन के लिए है। पहली अवैध संपत्ति पर अपवाद फेंकने के बजाय, संपूर्ण कक्षा को मान्य करें ताकि उपभोक्ता समझ सके कि पेलोड क्यों अमान्य है। एपीआई खोजने के लिए यह एक बहुत अच्छा तरीका है। – Brandon

1

आप

List<Callable<T>> tasks 
के रूप में कई Taska प्रतिनिधित्व कर सकते हैं

तो अगर आप कंप्यूटर चाहते हैं वास्तव में उन्हें समानांतर उपयोग में क्या करना

ExecutorService executorService = .. initialize executor Service 
List<Future<T>> results = executorService.invokeAll () ; 

अब आप पुनरावृति कर सकते हैं परिणामों के माध्यम से।

try 
{ 
    T val = result . get () ; 
} 
catch (InterruptedException cause) 
{ 
    // this is not the exception you are looking for 
} 
catch (ExecutionExeception cause) 
{ 
    Throwable realCause = cause . getCause () // this is the exception you are looking for 
} 

तो वास्तविक कारण (यदि यह अस्तित्व में है) जो भी इसके संबंधित कार्य में फेंक दिया गया अपवाद है।

+0

यह देखना अच्छा लगता है कि कार्यों को एक साथ चलाने के पहले से ही तरीके हैं। हालांकि, आपका समाधान कई कार्य विफलताओं का प्रतिनिधित्व करने वाले अपवाद को फेंकने का पता नहीं लगाता है। –

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