{"id":113,"date":"2007-12-13T09:20:00","date_gmt":"2007-12-13T09:20:00","guid":{"rendered":"http:\/\/www.tutego.de\/blog\/javainsel\/?p=113"},"modified":"2007-12-13T09:20:00","modified_gmt":"2007-12-13T09:20:00","slug":"warum-catch-exception-e-schlecht-ist-und-was-man-dagegen-tun-kann","status":"publish","type":"post","link":"https:\/\/www.tutego.de\/blog\/javainsel\/2007\/12\/warum-catch-exception-e-schlecht-ist-und-was-man-dagegen-tun-kann\/","title":{"rendered":"Warum catch ( Exception e ) schlecht ist, und was man dagegen tun kann"},"content":{"rendered":"<p>Eine ganze Reihe von Java-Methoden l\u00f6sen eine ganze Reihe von unterschiedlichen Ausnahmen aus:<o:p> <\/o:p>  <\/p>\n<p class=\"MsoNormal\" style=\"\"><b><span style=\"\" lang=\"EN-GB\">private<\/span><\/b><span style=\"\" lang=\"EN-GB\"> <\/span><b><span style=\"\" lang=\"EN-GB\">void<\/span><\/b><span style=\"\" lang=\"EN-GB\"> setLaf()<\/span><span style=\"\" lang=\"EN-GB\"><o:p><\/o:p><\/span><\/p>\n<p class=\"MsoNormal\" style=\"\"><span style=\"\" lang=\"EN-GB\">{<\/span><span style=\"\" lang=\"EN-GB\"><o:p><\/o:p><\/span><\/p>\n<p class=\"MsoNormal\" style=\"\"><span style=\"\" lang=\"EN-GB\"><span style=\"\">  <\/span><\/span><b><span style=\"\" lang=\"EN-GB\">try<\/span><\/b><span style=\"\" lang=\"EN-GB\"><o:p><\/o:p><\/span><\/p>\n<p class=\"MsoNormal\" style=\"\"><span style=\"\" lang=\"EN-GB\"><span style=\"\">  <\/span>{<\/span><span style=\"\" lang=\"EN-GB\"><o:p><\/o:p><\/span><\/p>\n<p class=\"MsoNormal\" style=\"\"><span style=\"\" lang=\"EN-GB\"> UIManager.<i>setLookAndFeel<\/i>( UIManager.<i>getSystemLookAndFeelClassName<\/i>() );<\/span><span style=\"\" lang=\"EN-GB\"><o:p><\/o:p><\/span><\/p>\n<p class=\"MsoNormal\" style=\"\"><span style=\"\" lang=\"EN-GB\"><span style=\"\">  <\/span>}<\/span><span style=\"\" lang=\"EN-GB\"><o:p><\/o:p><\/span><\/p>\n<p class=\"MsoNormal\" style=\"\"><span style=\"\" lang=\"EN-GB\"><span style=\"\">  <\/span><\/span><b><span style=\"\" lang=\"EN-GB\">catch<\/span><\/b><span style=\"\" lang=\"EN-GB\"> ( ClassNotFoundException e )<\/span><span style=\"\" lang=\"EN-GB\"><o:p><\/o:p><\/span><\/p>\n<p class=\"MsoNormal\" style=\"\"><span style=\"\" lang=\"EN-GB\"><span style=\"\">  <\/span>{<\/span><span style=\"\" lang=\"EN-GB\"><o:p><\/o:p><\/span><\/p>\n<p class=\"MsoNormal\" style=\"\"><span style=\"\" lang=\"EN-GB\"><span style=\"\">  <\/span>handle();<\/span><span style=\"\" lang=\"EN-GB\"><o:p><\/o:p><\/span><\/p>\n<p class=\"MsoNormal\" style=\"\"><span style=\"\" lang=\"EN-GB\"><span style=\"\">  <\/span>}<\/span><span style=\"\" lang=\"EN-GB\"><o:p><\/o:p><\/span><\/p>\n<p class=\"MsoNormal\" style=\"\"><span style=\"\" lang=\"EN-GB\"><span style=\"\">  <\/span><\/span><b><span style=\"\" lang=\"EN-GB\">catch<\/span><\/b><span style=\"\" lang=\"EN-GB\"> ( InstantiationException e )<\/span><span style=\"\" lang=\"EN-GB\"><o:p><\/o:p><\/span><\/p>\n<p class=\"MsoNormal\" style=\"\"><span style=\"\" lang=\"EN-GB\"><span style=\"\">  <\/span>{<\/span><span style=\"\" lang=\"EN-GB\"><o:p><\/o:p><\/span><\/p>\n<p class=\"MsoNormal\" style=\"\"><span style=\"\" lang=\"EN-GB\"><span style=\"\">  <\/span>handle();<\/span><span style=\"\" lang=\"EN-GB\"><o:p><\/o:p><\/span><\/p>\n<p class=\"MsoNormal\" style=\"\"><span style=\"\" lang=\"EN-GB\"><span style=\"\">  <\/span>}<\/span><span style=\"\" lang=\"EN-GB\"><o:p><\/o:p><\/span><\/p>\n<p class=\"MsoNormal\" style=\"\"><span style=\"\" lang=\"EN-GB\"><span style=\"\">  <\/span><\/span><b><span style=\"\" lang=\"EN-GB\">catch<\/span><\/b><span style=\"\" lang=\"EN-GB\"> ( IllegalAccessException e )<\/span><span style=\"\" lang=\"EN-GB\"><o:p><\/o:p><\/span><\/p>\n<p class=\"MsoNormal\" style=\"\"><span style=\"\" lang=\"EN-GB\"><span style=\"\">  <\/span>{<\/span><span style=\"\" lang=\"EN-GB\"><o:p><\/o:p><\/span><\/p>\n<p class=\"MsoNormal\" style=\"\"><span style=\"\" lang=\"EN-GB\"><span style=\"\">     <\/span>handle();<\/span><span style=\"\" lang=\"EN-GB\"><o:p><\/o:p><\/span><\/p>\n<p class=\"MsoNormal\" style=\"\"><span style=\"\" lang=\"EN-GB\"><span style=\"\">  <\/span>}<\/span><span style=\"\" lang=\"EN-GB\"><o:p><\/o:p><\/span><\/p>\n<p class=\"MsoNormal\" style=\"\"><span style=\"\" lang=\"EN-GB\"><span style=\"\">  <\/span><\/span><b><span style=\"\" lang=\"EN-GB\">catch<\/span><\/b><span style=\"\" lang=\"EN-GB\"> ( UnsupportedLookAndFeelException e )<\/span><span style=\"\" lang=\"EN-GB\"><o:p><\/o:p><\/span><\/p>\n<p class=\"MsoNormal\" style=\"\"><span style=\"\" lang=\"EN-GB\"><span style=\"\">  <\/span>{<\/span><span style=\"\" lang=\"EN-GB\"><o:p><\/o:p><\/span><\/p>\n<p class=\"MsoNormal\" style=\"\"><span style=\"\" lang=\"EN-GB\"><span style=\"\">  <\/span>handle();<\/span><span style=\"\"><o:p><\/o:p><\/span><\/p>\n<p class=\"MsoNormal\" style=\"\"><span style=\"\"><span style=\"\">  <\/span>}<\/span><span style=\"\"><o:p><\/o:p><\/span><\/p>\n<p class=\"MsoNormal\" style=\"\"><span style=\"\">}<\/span><span style=\"\"><o:p><\/o:p><\/span><\/p>\n<p class=\"MsoNormal\"><o:p> <\/o:p><br \/>Oftmals befindet sich dann im Quellecode eine Anweisung wie catch ( Exception e ), die gro\u00dfe Kaskaden von catch-Anweisungen verkleinern soll. (So kann man auch den gleichen Handler verwenden.) Das Problem dabei: Man f\u00e4ngt mehr, als man will, insbesondere wertvolle RuntimeExceptions, die man ungern als checked SQL- oder sonstwas-Exeption vernudelt haben m\u00f6chte. Wenn ich mir moderne Frameworks, wie Spring oder Java EE 5 anschaue, wird dort ausschlie\u00dflich mit RuntimeException gearbeitet, und die f\u00e4ngt eine catch ( Exception e ) ebenfalls brutal ab. Wenn zum Beispiel das Framework aufgrund einer unchecked Exception eine Transaktion abbricht, wir diesen Fehler aber schon \u201eweggefangen\u201c haben, ist das ein echtes Problem.<o:p><\/o:p><\/p>\n<p class=\"MsoNormal\">Ich sehe f\u00fcr eine vern\u00fcnftige Behandlung mehrere M\u00f6glichkeiten:<o:p><\/o:p><\/p>\n<ul>\n<li>Warten auf Java 3000, da dort sicherlich so etwas wie catch ( IOException, SQLException e ) definiert wird.<o:p><\/o:p><\/li>\n<li>Eine gemeinsame handle()-Funktion zu definieren, die man dann aus den catch-Handlern ansteuert.<\/li>\n<li>Ein catch ( Exception e ) und dann ein instanceof Test auf die erwarteten Exceptions mit einer re-thow, wenn der Typ nicht bekannt ist.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Eine ganze Reihe von Java-Methoden l\u00f6sen eine ganze Reihe von unterschiedlichen Ausnahmen aus: private void setLaf() { try { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() ); } catch ( ClassNotFoundException e ) { handle(); } catch ( InstantiationException e ) { handle(); } catch ( IllegalAccessException e ) { handle(); } catch ( UnsupportedLookAndFeelException e ) { handle(); } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","_links_to":"","_links_to_target":""},"categories":[1],"tags":[],"class_list":["post-113","post","type-post","status-publish","format-standard","hentry","category-allgemein"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts\/113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/comments?post=113"}],"version-history":[{"count":0,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts\/113\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/media?parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/categories?post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/tags?post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}