设计模式 - Exception设计

1. Java异常分类

Java异常顶层是Throwable , 下来分为了ExceptionError , 这个是JDK源码包的范畴 , 不代表第三方包的规则,

Throwable

The Throwable class is the superclass of all errors and exceptions in the Java language.

Throwable是Java语言异常的超类

For the purposes of compile-time checking of exceptions, Throwable and any subclass of Throwable that is not also a subclass of either RuntimeException or Error are regarded as checked exceptions.

为了在编译时检查异常, Throwable和任何未被申明RuntimeException或Error的子类 的Throwable子类都被视为被检查的异常对象。

设计模式 - Exception设计

Error

其中 Error 更多的表现为JVM内部异常, 可以称之为系统异常, 为Java程序不可处理的异常 ,不可用try-catch语句抓取的 , 比如常见的StackOverflowError(栈内存溢出) 和OutOfMemoryError (堆内存溢出)这两种很常见, 其他的还有很多,

Exception

这个就是我们常见的异常的超级父类了, 他为程序可以抓取的异常 ,此时可能表现在

The class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions.

程序会自动检测非RuntimeExceptionException的子类, 并且提示错误

Checked exceptions need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.

这些异常必须在方法上或者构造方法上用throws声明抛出去 ,

以上就是我们常见的两大父类对象

2. RuntimeException

RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine. RuntimeException and its subclasses areuncheckedexceptions. Unchecked exceptions do not need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.

RuntimeException 是在JVM正常运行期间可以抛出的异常的超类 .

RuntimeException及其子类是未检查异常。

未检查的异常不需要在方法或构造函数的抛出子句中声明,如果它们可以由方法或构造函数的执行抛出并传播到方法或构造函数边界之外。

    public static void main(String[] args) {
        try {
            testError();
        } catch (Exception e) {
            System.out.println(e.getMessage()); // 会输出 error
        }
    }

    static void testError() throws Exception{
        error();
    }

    static void error(){
        try {
            int i = 1 / 0;
        } catch (Exception e) {
            throw new RuntimeException("error");
        }
    }

异常会具有传递性, 类似于 trace一样, 出现后会找到你throws那里 , 所以不怕断链,

3. 构造方法

protected Throwable(String message,
                    Throwable cause,
                    boolean enableSuppression,
                    boolean writableStackTrace)

Constructs a new throwable with the specified detail message, cause, suppression enabled or disabled, and writable stack trace enabled or disabled.Ifsuppression isdisabled,getSuppressed forthisobjectwill returna zero-length array andcalls to addSuppressed that would otherwise append an exception to the suppressed list will have noeffect. If the writable stack trace is false, this constructor will not call fillInStackTrace(), a null will be written to the stackTrace field, and subsequent calls to fillInStackTrace and setStackTrace(StackTraceElement[]) will not set the stack trace. If the writable stack trace is false, getStackTrace will return a zero length array.

Note that the other constructors of Throwable treat suppression as being enabled and the stack trace as being writable. Subclasses of Throwable should document any conditions under which suppression is disabled and document conditions under which the stack trace is not writable. Disabling of suppression should only occur in exceptional circumstances where special requirements exist, such as a virtual machine reusing exception objects under low-memory situations. Circumstances where a given exception object is repeatedly caught and rethrown, such as to implement control flow between two sub-systems, is another situation where immutable throwable objects would be appropriate.

主要是知道writableStackTrace 当设置为 true的时候会追踪到出现异常的源头 , 而不是我们手动抛出的地方

默认是 false.

4. 设计异常

一般来说 ,我们的最内部的核心接口(或者抽象方法)最好继承与Exception , 如果外部我们已经知道我们自定的异常了 , 那么内部处理的异常可以使用RuntimeException ,

对于用户不知道的操作,最好申明异常, 对于接口的定义全部要throws 抛出异常 , 让使用者来处理 , 而不是你来处理 , 对于程序停止的 , 异常对于程序有正常运行有影响需要使用e.printstack , 最好打印出去, 然后使用Runtime.getRuntime(). hook 或者 exit 都可以 强制停止 ,

5. 总结

对于异常设计上 , 一般对于我们比如说没人帮我们去抓取异常, 此时就用throws,

展开阅读全文

页面更新:2024-04-26

标签:子类   函数   异常   接口   对象   内存   声明   常见   模式   程序   方法

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top