Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Averting checked exceptions [closed]

Tags:

java

exception

First, it is my strong opinion that the terms checked vs unchecked exceptions are imprecise, as it descriptively misleads who is doing the checking or if the checking is voluntary. Therefore I would be using the following terms in this question, so that people not aware of the difference might chip in an answer:

  • Check-mandated exception = checked exception
  • Check-not-mandated exception = unchecked exception

Motivation of Question:
I feel that the writers of standard Java utilities have committed misguided and inappropriate over-dependence on exceptions. The utility that is of my particular attention is the Integer.parseint(String) utility method.

  1. It should not use a check-not-mandated exception, because everyone who have used the util knows that the string supplied is often unknown and therefore, we would compel a try block anyway. Therefore, it is not a non-salvageable situation.
  2. If the writer of the utility had forced the use of check-mandated exception, it still would be inappropriate because since you know that there is a high likelihood of exception, then don't make it an exception. Since by logical reasoning, what you do expect would happen cannot be linguistically called an "exception".
  3. Reserve use of any exception for actually critical issues.

Question:
If I am to rewrite the utility, to satisfy my annoyance against overuse of exceptions by utility writers, what would be the best way to go around this predicament? That would treat unacceptable string, but satisfying my ideology against the overuse of exceptions.

My immediate thinking to solving it would be

  • Integer parse(String, Delegate)

Since Java is debilitated/impoverished by the absence of Delegates in the language, I would have it as

  • Integer parse(String value, CallBack unacceptedParseCallback);

Where

public interface CallBack<T,S> {
  T mitigate(S value);
}
  • Callback can be null, but only then would a NumberFormatException thrown if Callback mitigation is needed but not supplied.

However, I doubt my quick-and-dirty solution is the best way of avoiding the use of exceptions. What are the alternative algorithms I could use to solve my ideological disagreement with the over-dependence on exceptions? Before I embark on writing a library of such utilities.

like image 533
Blessed Geek Avatar asked Jul 21 '26 04:07

Blessed Geek


1 Answers

What you are looking for is the Maybe monad, implemented as the Optional interface in Java 8. This is a battle-proven approach which facilitates composition of functions in the face if possible "exceptions" anywhere within the composition chain.

One caveat with the above is that the complete API must support the Maybe monad for it to be truly useful.

like image 160
Marko Topolnik Avatar answered Jul 23 '26 18:07

Marko Topolnik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!