Java – avoid checking exceptions

First of all, I strongly believe that the inspection clause and the unchecked exception are inaccurate because it descriptively misleads who is conducting the inspection or whether the inspection is voluntary Therefore, I will use the following terms in this question so that people who do not understand the differences may answer:

>Check forced exception = checked exception > check not required exception = unchecked exception

Motivation for the problem: I think the writers of standard Java utilities have misled and unduly relied on exceptions The utility I'm particularly interested in is integer ParseInt (string) utility method

>It should not use the check not required exception, because everyone using util knows that the supplied string is usually unknown, so we will force a try anyway Therefore, this is not an irreparable situation. > If the writer of the utility forces the use of checking for forced exceptions, it is still inappropriate because you know that exceptions are highly likely, so don't make them an exception Because through logical reasoning, what you expect to happen cannot be called an "exception" in language Any exceptions to actual critical issues are reserved

Question: if I want to rewrite the utility to meet my worry about the excessive use of exceptions by utility writers, what is the best way to solve this dilemma? This will treat unacceptable strings, but meet my ideological opposition to overuse exceptions

My direct idea to solve this problem is

>Integer parsing (string, delegate)

I will do this because Java has become weak / poor due to the absence of representatives in the Java language

>Integer parsing (string value, callback unacceptedparsecallback);

where?

public interface CallBack<T,S> {
  T mitigate(S value);
}

>Callback can be null, but numberformatexception will be thrown only when callback mitigation is required but not provided

However, I doubt that my quick and dirty solution is the best way to avoid using exceptions What alternative algorithms can I use to resolve my ideological differences over over reliance on anomalies? Before I started writing such a utility library

Solution

What you are looking for is maybe monad, which is implemented as an optional interface in Java 8 This is a proven method. If "abnormality" may occur anywhere in the combination chain, it will promote the combination of facial functions

A warning mentioned above is that the complete API must support maybe monad to be really useful

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>