How to void checked exceptions in Java?
Posted
by
deamon
on Programmers
See other posts from Programmers
or by deamon
Published on 2014-06-02T07:25:27Z
Indexed on
2014/06/02
9:49 UTC
Read the original article
Hit count: 308
I consider checked exception for a design mistake in the Java language. They lead to leaky abstractions and a lot of clutter in the code. It seems that they force the programmer to handle exceptions early although they are in most cases better handled lately.
So my question is how to avoid checked exception? My idea is to execute the actual code inside an exception translator using lambda expressions. Example:
ExceptionConverter.convertToRuntimeException(() => {
// do things that could throw checked exceptions here
});
If for example a IOException
occurs it gets rethrown as an exception with the same name but from a different class hierarchy (based on RuntimeException
). This approach would effectivly remove the need to handle or declare checked exceptions. Exceptions could then be handled where and if it makes sense.
Another solution would be to declare IOException throws Exception
on each method.
What do you think which solution is better? Do you know any better approach to avoid (suppress) checked exceptions in Java?
© Programmers or respective owner