异常注意事项_多异常的捕获处理和异常注意事项_finally有return语句
异常注意事项_多异常的捕获处理
多个异常使用捕获又该如何处理呢?
1. 多个异常分别处理
2. 多个异常一次捕获,多次处理
3. 多个异常一次捕获一次处理
public class Demo01Exception {
public static void main(String[] args) {
//1. 多个异常分别处理。
/* try {
int[] arr = {1,2,3};
System.out.println(arr[3]);//ArrayIndexOutOfBoundsException: 3
}catch (ArrayIndexOutOfBoundsException e){
System.out.println(e);
}
try{
List<Integer> list = List.of(1, 2, 3);
System.out.println(list.get(3));//IndexOutOfBoundsException: Index 3 out-of-bounds for length 3
}catch (IndexOutOfBoundsException e){
System.out.println(e);
}*/
//2. 多个异常一次捕获,多次处理。
/*try {
int[] arr = {1,2,3};
//System.out.println(arr[3]);//ArrayIndexOutOfBoundsException: 3
List<Integer> list = List.of(1, 2, 3);
System.out.println(list.get(3));//IndexOutOfBoundsException: Index 3 out-of-bounds for length 3
}catch (ArrayIndexOutOfBoundsException e){
System.out.println(e);
}catch (IndexOutOfBoundsException e){
System.out.println(e);
}*/
/*
一个try多个catch注意事项:
catch里边定义的异常变量,如果有子父类关系,那么子类的异常变量必须写在上边,否则就会报错
ArrayIndexOutOfBoundsException extends IndexOutOfBoundsException
*/
/*try {
int[] arr = {1,2,3};
//System.out.println(arr[3]);//ArrayIndexOutOfBoundsException: 3
List<Integer> list = List.of(1, 2, 3);
System.out.println(list.get(3));//IndexOutOfBoundsException: Index 3 out-of-bounds for length 3
}catch (IndexOutOfBoundsException e){
System.out.println(e);
}catch (ArrayIndexOutOfBoundsException e){
System.out.println(e);
}*/
//3. 多个异常一次捕获一次处理。
/*try {
int[] arr = {1,2,3};
//System.out.println(arr[3]);//ArrayIndexOutOfBoundsException: 3
List<Integer> list = List.of(1, 2, 3);
System.out.println(list.get(3));//IndexOutOfBoundsException: Index 3 out-of-bounds for length 3
}catch (Exception e){
System.out.println(e);
}*/
//运行时异常被抛出可以不处理。即不捕获也不声明抛出。
//默认给虚拟机处理,终止程序,什么时候不抛出运行时异常了,在来继续执行程序
int[] arr = {1,2,3};
System.out.println(arr[3]);//ArrayIndexOutOfBoundsException: 3
List<Integer> list = Arrays.asList(1,2,3);
System.out.println(list);
System.out.println(list.get(3));//IndexOutOfBoundsException: Index 3 out-of-bounds for length 3
System.out.println("后续代码!");
}
}
如果finally有return语句,永远返回finally中的结果,避免该情况.
public class Demo02Exception {
public static void main(String[] args) {
int a = getA();
System.out.println(a);
}
//定义一个方法,返回变量a的值
public static int getA(){
int a = 10;
try{
return a;
}catch (Exception e){
System.out.println(e);
}finally {
//一定会执行的代码
a = 100;
return a;
}
}
}
异常注意事项_多异常的捕获处理和异常注意事项_finally有return语句的更多相关文章
- 0016 Java学习笔记-异常-如果try-catch-finally中都存在return语句会怎样?
上午在搜索"System.runFinalization"的时候,搜到 http://www.cnblogs.com/Skyar/p/5962253.html ,其中有关于try- ...
- 如何捕获access violation异常
文章目录 access violation的由来 access violation的实例 Win32 exception SEH异常与C++标准异常 捕获方法 1.access violation的由 ...
- iOS 捕获系统外异常
iOS 捕获系统外异常 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太 ...
- C# 语言规范_版本5.0 (第16章 异常)
1. 异常 C# 中的异常用于处理系统级和应用程序级的错误状态,它是一种结构化的.统一的和类型安全的处理机制.C# 中的异常机制非常类似于 C++ 的异常机制,但是有一些重要的区别: 在 C# 中,所 ...
- struts2捕获action类异常
首先是STRUTS.XML的配置.重点在于配置文件: <!-- struts2捕获action类异常 --> <global-results> <resu ...
- QT中使用google breakpad捕获程序崩溃异常
今天给大家介绍一个在linux下如何捕获程序崩溃异常的方法 一.google breakpad源码的下载和编译 1.https://github.com/google/breakpad.git,源码地 ...
- Java方法中捕获多个异常的处理机制
/** * @author wangyunhan * @throws Exception */ public static void main(String[] argßs) throws Excep ...
- python怎样在一行中捕获多个异常
所属网站分类: python基础 > 异常处理 作者:浮沉 链接:http://www.pythonheidong.com/blog/article/71/ 来源:python黑洞网,专注pyt ...
- [19/03/22-星期五] 异常(Exception)(二)_捕获异常
一.概念 捕获异常是通过3个关键词来实现的:try-catch-finally.用try来执行一段程序,如果出现异常,系统抛出一个异常,可以通过它的类型来捕捉(catch)并处理它, 最后一步是通过f ...
随机推荐
- OpenHarmony 3.1 Beta版本关键特性解析——ArkUI容器类API介绍
(以下内容来自开发者分享,不代表 OpenHarmony 项目群工作委员会观点) 刘鑫 容器类,顾名思义就是存储的类,用于存储各种数据类型的元素,并具备一系列处理数据元素的方法.在 ArkUI 开发框 ...
- 数据库-mysql索引篇
点赞再看,养成习惯,微信搜索「小大白日志」关注这个搬砖人. 文章不定期同步公众号,还有各种一线大厂面试原题.我的学习系列笔记. mysql的索引类型? mysql中有5种索引:普通索引.唯一索引.主键 ...
- WorkCount
此项目码云链接 软件测试方法和技术第二次作业 1 作业内容 根据WordCount的需求描述,先编程实现,再编写单元测试,最后撰写博客.至少完成需求中的基本功能. 2 WordCount需求说明 Wo ...
- [题解] 序列(sequence)
题目大意 给定一个长度为 \(N\) 的非负整数序列 \(A_1,A_2, \ldots ,A_N\),和一个正整数 \(M\).序列 \(A\) 满足 \(\forall 1 \le i \ ...
- Event Loop 是什么?
Event Loop 是什么? 本文写于 2020 年 12 月 6 日 广义上来说 Event Loop 并不是 JavaScript 独有的概念,他是一个计算机的通用概念. 狭义上来说,只有 No ...
- 力扣算法:125-验证回文串,131-分割回文串---js
LC 125-验证回文串 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 注:回文串是正着读和反着读都一样的字符串. ...
- 吊炸天,Spring Security还有这种用法!
在用Spring Security项目开发中,有时候需要放通某一个接口时,我们需要在配置中把接口地址配置上,这样做有时候显得麻烦,而且不够优雅.我们能不能通过一个注解的方式,在需要放通的接口上加上该注 ...
- OAuth2.0之OLTU实现举例
一.场景 三个角色:用户(user),web应用(client),资源服务器和授权服务器合为服务器(server) 用户登录登录后可查看自己的信息 二.准备 2.1 数据库 schema drop t ...
- linux篇-linux iptables配置
1 iptables默认系统自带 setup 2重启防火墙 /etc/init.d/iptables restart 3接受端口 Vi /etc/sysconfig/iptables -A INPUT ...
- 软件推荐 ---一款优秀的通信组件 HP_Socket
* HP-Socket 官方网站:http://www.jessma.org* HP-Socket 项目主页:http://www.oschina.net/p/hp-socket* HP-Socket ...