try catch块创建干净代码的最佳实践是什么?

uth*_*mas 10 java exception-handling

可能重复:
JAVA或C#中的异常管理的最佳实践

我今天早些时候在stackoverflow上读过一个问题,它让我思考处理异常的最佳实践是什么.

所以,我的问题是处理异常以产生干净和高质量代码的最佳实践什么.

这是我的代码,我认为它很安静,但如果我错了或不清楚,请告诉我!我试图在方法中牢记可测试性和相同的抽象级别.

每一条建设性意见都受到欢迎.:)

import java.awt.Point;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Preconditions;

/**
 * <p>This is a dummy code.</p>
 * The aim is present the best practice on exception separation and handling.
 */
public class ExceptionHandlingDemo {
    // System.out is not a good practice. Using logger is good for testing too (can be checked if the expected error messages are produced).
    private Logger logger = LoggerFactory.getLogger(ExceptionHandlingDemo.class);

    // instance of cannot work with List<Point>
    private interface PointList extends List<Point> {}

    /**
     * The method that loads a list of points from a file.
     * @param path - The path and the name of the file to be loaded.
     * Precondition: path cannot be {@code null}. In such case {@link NullPointerException} will be thrown. 
     * Postcondition: if the file don't exist, some IOException occurs or the file doesn't contain the list the returned object is {@code null}.
     * */
    /* (Google throws NullpointerExceptio) since it is not forbidden for the developers to throw it. I know this is arguable but this is out of topic for now. */
    public List<Point> loadPointList(final String path) {
        Preconditions.checkNotNull(path, "The path of the file cannot be null");

        List<Point> pointListToReturn = null;
        ObjectInputStream in = null;

        try {
            in = openObjectInputStream(path);
            pointListToReturn = readPointList(in);
        } catch (final Throwable throwable) {
            handleException(throwable);
        } finally {
            close(in);
        }

        return pointListToReturn;
    }

    /*======== Private helper methods by now ========*/

    private ObjectInputStream openObjectInputStream(final String filename) throws FileNotFoundException, IOException {
        return new ObjectInputStream(new FileInputStream(filename));
    }

    private List<Point> readPointList(final ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
        final Object object = objectInputStream.readObject();

        List<Point> ret = null;

        if (object instanceof PointList) {
            ret = (PointList) object;
        }
        return ret;
    }

    private void handleException(final Throwable throwable) {
        // I don't know the best practice here ...
        logger.error(throwable.toString());
    }

    private void close(final Closeable closeable) { 
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                logger.error("Failed closing: %s", closeable);
            }
        }
    }

    /*======== Getters and setters by now. ========*/

    // ...

    /**
     * @param args
     */
    public static void main(String[] args) {
        ExceptionHandlingDemo test = new ExceptionHandlingDemo();
        test.loadPointList("test-filename.ext");
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

我想避免的是在彼此之后写下大量的捕获案例......

Whi*_*g34 5

乍一看有些建议:

  1. 您不应该捕获Throwable并尽可能捕获特定的异常.捕获的麻烦Throwable在于包括Error诸如此类OutOfMemoryError的类.你想让那些通过(他们因为一个原因而未经检查).
  2. 记录异常时,总是传递异常,而不仅仅是异常toString().没有堆栈跟踪就很难诊断出问题.
  3. 您可能不需要一般的异常处理方法.

所以在你捕获异常的地方,你想要这样的东西:

} catch (IOException e) {
    logger.error("some relevant message", e);
    // now handle the exception case
}
Run Code Online (Sandbox Code Playgroud)

如果可能,该消息应包括一些上下文信息.有人在搜索日志时可能有助于追踪问题的任何内容.


art*_*tol 3

对于我不会打扰的检查异常,我总是使用 org.apache.commons.lang.UnhandledException。

例如

/**
 * Calls Thread.sleep(millis)
 */
public static void threadSleep(final long millis) {
    try {
        Thread.sleep(millis);
    } catch (final InterruptedException e) {
        throw new UnhandledException(e);
    }
}
Run Code Online (Sandbox Code Playgroud)