java.lang.Exception
वर्ग पर Javadocs से:क्या जावा का चेक अपवाद हैंडलिंग टूटा हुआ है?
जांचे हुए अपवादों एक विधि या निर्माता के में घोषित किए जाने की जरूरत खंड फेंकता है तो वे विधि के निष्पादन या निर्माता द्वारा फेंका जा सकता है और विधि या निर्माता सीमा के बाहर प्रचार ।
लेकिन इस कोड पर विचार करें:
package other;
public class CheckedExceptionHandling {
private static <E extends Exception> void throwException() throws E {
throw (E) new CheckedException2(); // unchecked cast warning
}
private static void setUncaughtExceptionHandler() {
Thread.currentThread().setUncaughtExceptionHandler((t, e) -> {
System.out.println("Unhandled exception: " + e.getClass()); // reports CheckedExceptionHandling$CheckedException2
});
}
public static void main(String[] args) /* no checked exceptions declared! */ {
setUncaughtExceptionHandler();
try {
CheckedExceptionHandling.<CheckedException1>throwException();
} catch (CheckedException1 e) {
System.out.println(e); // never gets here
}
}
// checked exceptions:
public static class CheckedException1 extends Exception {}
public static class CheckedException2 extends Exception {}
}
यह एक चेतावनी के साथ संकलित करता है तथा रन-टाइम परिणाम है:
क्रिया के अपवाद: वर्ग other.CheckedExceptionHandling $ CheckedException2
मुझे एक संकलन-समय त्रुटि unreported exception CheckedException2; must be caught or declared to be thrown
याकी उम्मीद थीया कम से कम ClassCastException
रन टाइम पर।
लेकिन कंपाइलर एक अपवाद अपवाद हैंडलर पर main
विधि के बाहर अनचाहे, अविकसित और प्रसारित करने के लिए एक चेक अपवाद को छोड़ने की अनुमति देता है।
क्यों? क्या मुझसे कोई चूक हो रही है?
प्लस वन। अच्छा अच्छी तरह से लिखा प्रश्न। जावा अपवाद हैंडलिंग टूटा हुआ है। आखिरकार आप इसका इस्तेमाल करेंगे और शांत हो जाएंगे। – Bathsheba
यहां कुछ भी टूटा नहीं है। यह सब spec के अनुसार है। – Tunaki
यह "स्नीकी फेंक" की तरह दिखता है। पसंद के अपने खोज इंजन पर शब्द की खोज करें, उदा। https://www.reddit.com/r/programming/comments/2x41h4/sneaky_exceptions_in_java/ –