异常情况

1、自定义异常(可参考下面的例子)

public class IOException extends Exception
{
    static final long serialVersionUID = 7818375828146090155L;

    public IOException()
    {
        super();
    }

    public IOException(String message)
    {
        super(message);
    }

    public IOException(String message, Throwable cause)
    {
        super(message, cause);
    }

    
    public IOException(Throwable cause)
    {
        super(cause);
    }
}

2、finally问题

(1)finally中的return 会覆盖 try 或者catch中的返回值。

(2)finally中的return会抑制(消灭)前面try或者catch块中的异常

(3)finally中的异常会覆盖(消灭)前面try或者catch中的异常

3、建议

不要在fianlly中使用return。
不要在finally中抛出异常。
减轻finally的任务,不要在finally中做一些其它的事情,finally块仅仅用来释放资源是最合适的。
将尽量将所有的return写在函数的最后面,而不是try ... catch ... finally中。

猜你喜欢

转载自blog.csdn.net/sinat_16754927/article/details/79881462