Error Handling – A more functional test … catch construct in Java

I saw a question about Stack Overflow asking to review a custom build … catch that uses Optionals, and had the idea to try to write my own version. Mine does not use Optionalif. Each provided capture handler is supposed to return a value.

I realized about half of the writing of this that it was probably a terrible idea that should never be used in the real world for a variety of reasons. I have not written Java for a few years, however, and I am curious to know what can be improved.

Example of use:

Integer i =
new Try <> (() -> Integer.parseInt ("f"))
.catching (NumberFormatException.class, (e) -> 2)
.catching (IllegalStateException.class, (e) -> 3)
.andFinally (() -> System.out.println ("Finally!"));

// Finally!

Whole j =
new Try <> (() -> Integer.parseInt ("1"))
.catching (NumberFormatException.class, (e) -> 2)
.exécuter ();

System.out.println (i); // 2
System.out.println (j); // 1

Basically, it works is the Try object may have contagious methods chained on that registering handlers. When an exception is thrown, it looks for a matching handler and calls it by returning the returned value. If no manager is found, it renews the exception.

Major problems:

  • The need to .class is regrettable. I did not see how to ask the caller to indicate the exception class to intercept.

  • Non-exceptional classes can be registered, although this has no effect other than the minimal use of memory and processor.

  • The need to perform when and finally is not used. I do not see how the class would know that all capturees were added however.

  • The need to New is unhappy too. I thought I could take a Scala page and create a static method that plays the role of constructor, but I had to do something like: Try.tryMethod to reference it, which is not much better.

  • Check in and finally seems to be useless. Unless someone does something weird like:

    Try t = new Try <> (() -> null);
    t.and infinately (() -> {});
    t.and infinately (() -> {});
    

    It should not be possible to add more finally blocks. I do not know how much I should prevent the user from doing stupid things. I could make the object immutable, but again, I'm not sure it's necessary.

Since this is essentially a code of practice, I would like no matter what to everything that could be improved here. This is the first time that I have written that I wrote in recent years, so I am sure that many improvements can be made.

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

public class Try {
private provider tryExpr;
Private Card <Class, Function> handlers = new HashMap <> ();
Runnable private finallyExpr;

public Try (Provider tryExpr) {
this.tryExpr = tryExpr;
}

public essay catch (Class ex, Function manager) {
if (handlers.containsKey (ex)) {
throwing a new exception IllegalStateException ("exception" + ex.getSimpleName () + "has already been intercepted");

} other {
handlers.put (ex, handler);

return this;
}
}

public T execute () {
try {
return tryExpr.get ();

} catch (Exception e) {
if (handlers.containsKey (e.getClass ())) {
A function handler = handlers.get (e.getClass ());

return.apply manager (e);

} other {
launch e;
}

} finally {
if (finallyExpr! = null) {
finallyExpr.run ();
}
}
}

public T andFinally (Runnable newFinallyExpr) {
if (finallyExpr == null) {
finallyExpr = newFinallyExpr;

} other {
throw the new IllegalStateException ("can not have multiple expressions eventually");
}

return execute ();
}

}