tag:blogger.com,1999:blog-7735872642513631302.post5026156029178597374..comments2023-07-15T17:54:51.492+01:00Comments on Alexey Ragozin: Java, How to throw undeclared checked exceptionAlexey Ragozinhttp://www.blogger.com/profile/13720493857045012756noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-7735872642513631302.post-55102110180621391522011-11-02T07:53:14.808+00:002011-11-02T07:53:14.808+00:00Oops, sorry didn't get you quite right.
It w...Oops, sorry didn't get you quite right. <br /><br />It works OK, and throws anything if being called , say like "AnyThrow.throwUncheked(new IOException())"Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7735872642513631302.post-88740789349920775642011-11-01T23:04:30.453+00:002011-11-01T23:04:30.453+00:00Well, my Eclipse doesn't allow me to throw any...Well, my Eclipse doesn't allow me to throw anything but a RuntimeException or its descendant this way. And with such a limitation it doesn't break any java rules on exceptions handling.<br />Did you try this code with, say, IOException?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7735872642513631302.post-64795263273298476222011-11-01T20:35:42.368+00:002011-11-01T20:35:42.368+00:00it looks interesting but to write program in such ...it looks interesting but to write program in such manner would be a way to get a lot of pain imhoAnonymoushttps://www.blogger.com/profile/10916501254371406009noreply@blogger.comtag:blogger.com,1999:blog-7735872642513631302.post-48745852057322939052011-11-01T19:30:13.720+00:002011-11-01T19:30:13.720+00:00Famous trick. Also you can create default construc...Famous trick. Also you can create default constructor like this:<br /><br />public Some() throws Exception {<br /> throw new Exception();<br />}<br /><br />and execute from reflection:<br /><br />Some.class.newInstance();<br /><br />Result will be the same as yours.Enterprise developerhttps://www.blogger.com/profile/15100747566554349163noreply@blogger.comtag:blogger.com,1999:blog-7735872642513631302.post-29620337388681580422011-11-01T17:50:12.354+00:002011-11-01T17:50:12.354+00:00Nice, I added a twist on it:
http://www.eishay.com...Nice, I added a twist on it:<br />http://www.eishay.com/2011/11/throw-undeclared-checked-exception-in.html<br />So the compiler will know you're breaking the flow.Eishay Smithhttps://www.blogger.com/profile/09443096006184006852noreply@blogger.comtag:blogger.com,1999:blog-7735872642513631302.post-11219130881371666742011-10-31T18:16:54.150+00:002011-10-31T18:16:54.150+00:00It works, I have tested it :)
Trick is, E is a ge...It works, I have tested it :)<br /><br />Trick is, E is a generic. At runtime <a href="http://download.oracle.com/javase/tutorial/java/generics/erasure.html" rel="nofollow">erasure</a> takes place and E is replaced by Throwable (a bound for E). Forcing template agrument at throwUnchecked() does really add any code to compile class, but let's us skip <b>throws</b> declaration.Alexey Ragozinhttps://www.blogger.com/profile/13720493857045012756noreply@blogger.comtag:blogger.com,1999:blog-7735872642513631302.post-84181747472663544692011-10-31T17:19:47.914+00:002011-10-31T17:19:47.914+00:00i think u can still throw only uncheckedexceptions...i think u can still throw only uncheckedexceptions, else the cast (E)e would fail, isn't it?Guru Prasad G.V.https://www.blogger.com/profile/11821252827217534617noreply@blogger.com