0

I need make some code super stable and make sure all exceptions thrown within a block don't stop the code execution.

For now i have the following solution:

iterated.map(it -> {
   try {
      handleElement(it);
   } catch (Exception e) {
      handleError(e);
   }
});

But from the other side i feel like there should be more elegant way to do it. Please help me to find it.

4
  • What's not elegant about it?
    – user253751
    Jul 21, 2016 at 9:48
  • @immibis i just said it was a feeling :) and may be there are any new tools/utils which would allow to avoid quite ugly try/catch Jul 21, 2016 at 9:51
  • Your code will indeed catch "all exceptions," but it won't be very intelligent. One must have specific catches for specific exceptions. Just like in real life, wearing a seat belt will not prevent your car from running out of gas or you from starving to death. I think you will catch even null pointer exceptions in handleError(). Your code "won't stop" if you catch all exceptions, but maybe you don't want it to blindly continue in such a case (that could be much worse)! What do you expect to go wrong in handleElement()? Jul 22, 2016 at 2:03
  • @Fuhrmanator let's say i need to implement the processing of some array of elements. And in the end i need to provide a report of what was processed successfully or not. That's quite common case. From the other side, i totally understand what you mean about best practices of handling exception, but don't see any other way how to implement this. Jul 22, 2016 at 4:52

2 Answers 2

0

Well that's indeed how to call exceptions, but it's more important how to deal with these occurrences. An Exception should be thrown when the app experiences something which it could not expect. Just catching all exceptions will

  • Not make your code more stable, it just doesn't throw exceptions higher up
  • Leave errors as-is, and they will still be thrown (you probably want this)
  • Remove exceptions which you might want to have logged

Keep in mind what is a REAL exception for your app. Here are some examples:

  • You have a connection to a backend, which is unstable. You knew this, and made the app possible to reconnect and/or postpone the actions, or whatever in order for the app that this backend is in fact unstable:
    • This is NO exception, just log a message if needed.
  • The client misentered some information
    • NO exception, this happens all the time, the app should cope with that
  • After some code update you have a problem even setting up the connection
    • EXCEPTION: log it and fix the problem!
1
  • thanks for your answer, but the question here is not about handling exception in general in application, but about the particular piece of code, which i need to be stable. Jul 21, 2016 at 21:32
0

There is nothing wrong with you code and probably I would keep as is but if you like you can have a function that handles another function and catches the exceptions:

public class ExceptionCatcher<T, R> implements Function<T, R> {

    public final Function<T, R> function;

    public final Function<Exception, R> exceptionHandler;

    public ExceptionCatcher(Function<T, R> function, Function<Exception, R> exceptionHandler) {
        this.function = function;
        this.exceptionHandler = exceptionHandler;
    }

    @Override
    public R apply(T o) {
        try {
            return function.apply(o);
        }catch (Exception e){
            return exceptionHandler.apply(e);
        }

    }
};

and your code will look like:

ExceptionCatcher ex = new ExceptionCatcher(this::handleElement, this::handleError);
iterated.map(ex);

This is just an example; probably instead of a simple Consumer you want a BiFunction and map to a default value based on the exception and on the failing onject. Or return null and filter null after this. It depends a lot on what you want to achieve

2
  • thanks for your snipper, but it seems like your function misses the handleError function as a param. Generally, this approach remains the same, but wrapped into function Jul 21, 2016 at 9:52
  • @ikryvorotenko you can also take that as an argument when constructing it. It was intended as an example. you can build it as you wish Jul 21, 2016 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.