异常注意事项_多异常的捕获处理和异常注意事项_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 ...
随机推荐
- HttpServletResponse & HttpServletRequest
web服务器接收到客户端的http请求,针对这个请求,分别创建一个代表请求的HttpServletRequest对象,代表响应的一个HttpServletResponse: 如果要获取客户端请求过来的 ...
- 史上最全Linux面试题(2020最新版)
作者:ThinkWon 链接:https://blog.csdn.net/thinkwon/article/details/104588679 导读:本文整理了最新的Linux面试题,近3万字,约10 ...
- Docker容器的安装和使用
Docker容器的安装和使用 安装: curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun 或国内:curl -sSL ...
- 【面试普通人VS高手系列】Spring中事务的传播行为有哪些?
一个工作了2年的粉丝,私信了一个比较简单的问题. 说: "Spring中事务的传播行为有哪些?" 他说他能记得一些,但是在项目中基本上不需要配置,所以一下就忘记了. 结果导致面试被 ...
- Hadoop介绍篇
Hadoop详解 1.前言 对于初次接触Hadoop的小伙伴来说,Hadoop是一个很陌生的东西,尤其是Hadoop与大数据之间的关联,写这篇文章之前,我也有许多关于Hadoop与大数据的疑惑,接下来 ...
- 溢出属性,定位,z-index,JS
溢出属性 1.visible(默认值):使溢出内容展示 2.hidden:隐藏溢出内容且不出现滚动条 3.scroll:隐藏溢出容器的内容,溢出的内容可以通过滚动呈现 4.auto:与scroll没啥 ...
- vue-router实现原理及简易demo
自定义路由demo,git地址: git@github.com:xsk-walter/Vue-router.git 一.router基本使用 ① 创建和路由相关的组件 ②Vue.use(vueRout ...
- Linux磁盘空间查看及空间满的处理
问题 在部署应用到测试环境的时候,有些文件同步出错,最后定位到测试服务器空间满了. 解决 查看磁盘空间还剩多少空间 df -h 查看根目录下每个目录占用空间大小 du --max-depth=1 -h ...
- 好客租房46-react组件进阶目标
1能够使用props接收数据 2能够使用父子组件之间的通讯 3能够实现兄弟组件之间的通讯 4能够给组件添加props校验 5能够说出生命周期常用的钩子函数 6能够知道高阶组件的作用 组件通讯介绍 组件 ...
- WC2019
好题啊! 数树 \(\text{opt = 0, 6 pts.}\) 显然答案为 \(y^{n-|E_1∩E_2|}\) . \(\text{opt = 1, 47 pts.}\) \[\sum_{E ...