3.用Exception类捕获所有异常
马克-to-win:注意,一个事实是:Exception类是所有其他异常类的父类,所以Exception类能捕获所有的异常。马克-to-win:问题是用Exception来捕获所有的异常,这个技术有什么好处和缺点?好处就是:不管发生什么异常,都能进入Exception catch块儿,这样,程序都不崩溃。马克-to-win:缺点就是:不能根据特定的异常做特定的处理。马克-to-win:比如,对于以上例子,我们可以用以下的技术,Exception来捕获所有的异常,都可以达到程序不崩溃的目的。马克-to-win:因为Exception是所有其他异常的父类。
例:1.3.1-
public class Test {
public static void main(String[] args) {
int arg1;
int result;
String s="abc";
// String s="12";
try {
arg1 = Integer.parseInt(s);
result = arg1 /0;
System.out.println("try中完成finish");
}
catch (Exception e) {
System.out.println(e);
}
System.out.println("马克-to-win:优雅结束");
}
}
例:1.3.2-
public class Test {
public static void main(String[] args) {
int arg1;
int result;
// String s="abc";
String s="12";
try {
arg1 = Integer.parseInt(s);
result = arg1 /0;
System.out.println("try中完成finish");
}
catch (Exception e) {
System.out.println(e);
}
System.out.println("马克-to-win:优雅结束");
}
}
更多请见: https://blog.csdn.net/qq_44639795/article/details/103109504
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有