2012-11-14 23 views
18

Çok sayıda kontrol edilmiş istisna ekleyen bir ifadem var. Böyle hepsi için tüm catch bloğu ekleyebilirsiniz:Çalışma zamanı istisnaları dışında tüm özel durumları yakalamak mümkün mü?

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... 

ben kod tekrarına tür vardır ve ayrıca yazmak için kod bir sürü yani hepsi aynı şekilde ele alınır bu çünkü sevmiyorum. Bunun yerine Exception yakalamak olabilir: Tamam olur,

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

tüm çalışma zamanı istisnaları yakalanmadan atılır istiyorum hariç. Bunun herhangi bir çözümü var mı? Yakalanacak istisnanın türüne ilişkin bazı akıllı genel bildirimin hileyi (ya da belki de) yapamayacağını düşünüyordum.

cevap

40

Aşağıdaki yapabilirdi: Java 7 kullanabiliyorsa

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

iyi cevap ... 1 – Juvanis

+2

1 Serin. Gördüğüm en temiz değil ama hile yapar. – drasto

11

, kullanabileceğiniz bir Multi-Catch:

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

+1 Java 7 –

+0

+ 1'deki bu yeni değerli (ve gecikmiş IMO) özelliğiyle ilgili beni uyarıyor. Bu nedenle, Java 7'yi kullanabilseydim. Ama yapamıyorum ve bunun için çok üzgünüm çünkü bu Multi-Catch mükemmel. Java 6'yı kullanmalıyım: ((( – drasto

1

Sen, böyle bir şey deneyebilirsiniz temelde sonra her şey ve yakalamak için yeniden atmak o sınıfın bir örneğidir ise RuntimeException ...

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

Belki de ... farklı bir sınıfa

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

böyle bir şey kodunu taşımak Ve kullanmak istiyorum bunun üzerinde yazmaya dağınık tekrar tekrar (ve bakımı için kötü) olurdu sanki görünce Bu gibi kodu ...

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

biz hala MyCustomInitializationException özelleştirebilirsiniz böylece biz message geçmek olduğunu kaydeden.

+0

Birkaç kez yapmam gerekirse son seçenek iyi olurdu ama bu benim durumum değil - Sadece bir kez kullanmam gerekiyor. Sadece "String" ile statik yöntemi parametrelendirebilirim – drasto

+0

mesajında ​​"ex" kodunu "RuntimeException" koduna dökmeniz gerekir. –

İlgili konular