HiveBrain v1.2.0
Get Started
← Back to all entries
debugjavaMinor

Class for catching error and logging

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
loggingerrorcatchingforandclass

Problem

I often write something like this in my code:

try {
    obj.doSomething();
} catch (Throwable exception) {
    // there is no need to interrupt application
    String exceptionMessage = String.valueOf(exception.getMessage());
    Log.e("Name of my app", exceptionMessage, exception);
    // and may be something else
}


So I wrote this simple class:

public class TryHelper {

    public static final TryHelper LOGGER = new TryHelper(new Handler() {
        @Override
        public void handle(Throwable exception) {
            LogUtils.exception(exception);
        }
    });

    private final Handler exceptionHandler;

    public TryHelper(Handler exceptionHandler) {
        this.exceptionHandler = exceptionHandler;
    }

    public void tryDoIt(Runnable runnable) {
        try {
            runnable.run();
        } catch (Throwable exception) {
            exceptionHandler.handle(exception);
        }
    }

    public void tryDoIt(RunnableAbleToThrowCheckedException runnable) {
        try {
            runnable.run();
        } catch (Throwable exception) {
            exceptionHandler.handle(exception);
        }
    }

}


And interfaces from my own util package:

public interface RunnableAbleToThrowCheckedException {
    void run() throws Exception;
}

public interface Handler {
    void handle(T obj);
}


Using:

TryHelper.LOGGER.tryDoIt(new Runnable() {
    @Override
    public void run() {
        obj.doSomething();
    }
});


-
But I'm pretty bad at English. So I need your help in choosing proper names (instead of TryHelper, RunnableAbleToThrowCheckedException, tryDoIt(), etc.).

I'm a fan of Uncle Bob and his book "Clean Code":


We want the code to read like a top-down narrative.

-
And what do you think about the need for this class? Maybe it was a bad idea?

Solution

-
You could use Callable instead of RunnableAbleToThrowCheckedException. Its call method could throw Exeptions and has a return value too. You can use Void as a type parameter and return null if you don't need these features:

public class MyCallable implements Callable {

    @Override
    public Void call() throws Exception {
        ...
        return null;
    }
}


-
TryHelper seems like an Executor to me. I'd name it ErrorHandlerExecutor and rename its tryDoIt methods to submit (similar to ExecutorService). I guess most developers would find it familiar.

-
errorLoggerExecutor seems a fair name for an instance which logs the errors of the submitted tasks.

-
I would also rename Handler to ErrorHandler to be more descriptive.

Code Snippets

public class MyCallable implements Callable<Void> {

    @Override
    public Void call() throws Exception {
        ...
        return null;
    }
}

Context

StackExchange Code Review Q#57178, answer score: 7

Revisions (0)

No revisions yet.