设计模式Exception设计
1. Java异常分类
Java异常顶层是 Throwable , 下来分为了Exception 和Error , 这个是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子类都被视为被检查的异常对象。
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.
程序会自动检测非 RuntimeException 的Exception 的子类, 并且提示错误
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 are uncheckedexceptions . 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,
煮熟鸭子飞了!山东新星投奔刘维伟内讧再度展开巩大师早有预言今年夏天山东队按下了重组加速键,在王晗的带领下,各种大手笔的操作应运而生。首先在后卫防线签下了高诗岩杨林祎以及张辉三名球员,这就保证了山东队在未来35年内后卫防线拥有绝对的竞争力。
研究揭示天山造山带陆内变形机制陆内造山带是指发育在大陆板块内部远离汇聚板块边缘的造山带,其形成与演化涉及板块内部应力远程传递大陆岩石圈流变特性等基本科学问题。天山造山带作为中亚造山带塔里木哈萨克斯坦拼贴系的主体
POWERDAO算力界的灰度前言当前,新一轮科技革命和产业变革正在重塑全球经济结构,算力作为数字经济时代新的生产力,是支撑数字经济发展的坚实基础,对推动科技进步促进行业数字化转型以及支撑经济社会发展发挥重要的
台积电警告,低端芯片出现短缺由于诸多因素的影响,今年下半年芯片的价格开始出现暴跌。更有报道说芯片从2021年的200元价格暴跌到了20元。出现了芯片产能过剩,企业大量砍单的现象。大家都在潜意识里面有一个认知就
从3999跌至3489元,三星E5屏256GB蔡司影像,友商真香天玑9000很长一段时间以来,每次提到搭载联发科处理器的手机,很多网友都表示再见。但是今年以来,联发科发布的芯片,口碑都不错,都深受厂商和用户的喜爱,在上半年的安卓旗舰机中,联发科旗下的高端芯
京东快递滞留中,谁动了他的苹果?牛老板反映,五月份通过京东快递邮寄电脑给上海的客户,电脑一直没到,说是滞留了。后来他发现,电脑被激活了。视频京东快递滞留后退回,苹果为何激活了?牛老板我是电商卖家,在网上卖苹果的产
差价最高9000元,华为Mate50系列怎么选?配置详细对比一文看懂华为旗舰过几天就要上架了,有不少粉丝问我,买12999元的顶配版,影不影响他吃5块钱的泡面?我觉得是有影响的,因为你买了,可能就吃不起泡面了。本次华为Mate50系列共有四款型号,
2022。9。8学习笔记1做什么事一定要有核心竞争力,京东有物流,没支付淘宝有支付,没物流腾讯有支付,有廉价流量,没有物流pdd啥也没有,但有腾讯支持解决了支付问题每日优鲜走到今天这一步烧出巨大的历史债务
45张图带你从入门到精通学习WireShark你好,这里是网络技术联盟站。前几天文章中我们介绍了常用的抓包工具,有朋友表示用WireShark场景比较多,想让我写一篇有关WireShark详细的文章,那么今天瑞哥安排!一什么是
家电走衰!白电三雄,谁成真熊?作者星空下的锅包肉编辑菠菜的星空排版星空下的柠檬水今年上半年,家电行业形势依旧严峻。国内销售规模3609亿,同比下降11。2。出口规模2833亿,同比下降8。2。这背后,一方面是因
突发,乐视重出江湖但可能还欠200亿种种迹象表明退隐3年后,乐视又重出江湖,貌似过上了小康日子。今天,赶在苹果发布会的同一日,乐视也开了场发布会透露业务进展良好,今年上半年新增用户300万,新上电影电视剧400余部。