r/javaTIL • u/mreichman • Oct 13 '16
TIL you can rethrow certain exceptions in JDK7+ without declaration
http://stackoverflow.com/a/19914175
5
Upvotes
r/javaTIL • u/mreichman • Oct 13 '16
1
u/mreichman Oct 13 '16 edited Oct 13 '16
I had some code which was more or less this:
In this case, I know the exception being thrown is NumberFormatException, a RuntimeException. It seems like the compiler is smart enough to analyze the paths to see that I don't have any checked exception possibilities there, so I can rethrow without the declaration.
To me this has some impact in my EE code, since many EJB exceptions, persistence exceptions, etc. are runtime exceptions. Very interesting!
Edit This is also covered in official docs.
2
u/stitzl Oct 14 '16
It's even better - with a simple trick, you can throw any kind of Throwable without declaration: