异常注意事项_多异常的捕获处理

多个异常使用捕获又该如何处理呢?
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语句的更多相关文章

  1. 0016 Java学习笔记-异常-如果try-catch-finally中都存在return语句会怎样?

    上午在搜索"System.runFinalization"的时候,搜到 http://www.cnblogs.com/Skyar/p/5962253.html ,其中有关于try- ...

  2. 如何捕获access violation异常

    文章目录 access violation的由来 access violation的实例 Win32 exception SEH异常与C++标准异常 捕获方法 1.access violation的由 ...

  3. iOS 捕获系统外异常

    iOS 捕获系统外异常 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太 ...

  4. C# 语言规范_版本5.0 (第16章 异常)

    1. 异常 C# 中的异常用于处理系统级和应用程序级的错误状态,它是一种结构化的.统一的和类型安全的处理机制.C# 中的异常机制非常类似于 C++ 的异常机制,但是有一些重要的区别: 在 C# 中,所 ...

  5. struts2捕获action类异常

    首先是STRUTS.XML的配置.重点在于配置文件: <!-- struts2捕获action类异常 -->         <global-results> <resu ...

  6. QT中使用google breakpad捕获程序崩溃异常

    今天给大家介绍一个在linux下如何捕获程序崩溃异常的方法 一.google breakpad源码的下载和编译 1.https://github.com/google/breakpad.git,源码地 ...

  7. Java方法中捕获多个异常的处理机制

    /** * @author wangyunhan * @throws Exception */ public static void main(String[] argßs) throws Excep ...

  8. python怎样在一行中捕获多个异常

    所属网站分类: python基础 > 异常处理 作者:浮沉 链接:http://www.pythonheidong.com/blog/article/71/ 来源:python黑洞网,专注pyt ...

  9. [19/03/22-星期五] 异常(Exception)(二)_捕获异常

    一.概念 捕获异常是通过3个关键词来实现的:try-catch-finally.用try来执行一段程序,如果出现异常,系统抛出一个异常,可以通过它的类型来捕捉(catch)并处理它, 最后一步是通过f ...

随机推荐

  1. python学习-Day38-HTTP

    目录 HTTP简介 可以充当客户端的有哪些 HTTP 工作原理 HTTP协议 HTTP协议四大特性 数据格式 请求格式: 响应格式: HTTP 请求方法 HTTP 状态码分类 响应分为五类: HTTP ...

  2. 攻防世界-MISC:can_has_stdio?

    这是攻防世界MISC高手进阶区的题目,题目如下: 点击下载附件一,解压后得到一个txt文件,打开后内容如下: 根据百度搜索的结果可知这是一种叫做BrainFuck的语言,BrainFuck是由Urba ...

  3. JAVA IDEA连接mysql遇到的问题

    Mysql-connector-java驱动问题 因为缺乏驱动而无法成功连接数据库 下载驱动(教程) 相关网址 安装驱动 简单图示

  4. 4.25JMster环境搭建、webxml及测试平台练习

    1.Java环境搭建 右击电脑属性--高级设置--环境变量--系统变量--新建(输入JAVA_HOME.C:\Program Files\Java\jdk1.8.0_91---CLASSPATH..; ...

  5. 关于JS精度缺失问题

    问题描述 在Java后端传一个比较大的Long值的时候 前端接收值的时候会出现精度的缺失: 解决办法 添加一个转换类 点击查看代码 public class JacksonObjectMapper e ...

  6. skywalking 搭建链路监控

    一.skywalking简介 官网:https://github.com/apache/skywalking 引用官网的架构:  二.部署OAP和UI 需使用的镜像 apache/skywalking ...

  7. 【多线程】线程强制执行 join()

    线程强制执行 join() Join合并线程,待此线程执行完成后,再执行其他线程,其他线程阻塞 : 可以想象成插队. 代码示例: /** * @Description 测试join方法 * @Auth ...

  8. 找到占用CPU最高的Java线程

    一.找到java进程id jps查看当前运行的java进程id [root@localhost ~]# jps 18354 Jps 9381 Bootstrap 二.找到内存和CPU占用最高的线程pi ...

  9. 312. Burst Balloons - LeetCode

    Question https://leetcode.com/problems/burst-balloons/description/ Solution 题目大意是,有4个气球,每个气球上有个数字,现在 ...

  10. 好客租房7-React脚手架的使用

    3.3在脚手架中使用React //第一步导入react import React from "React" import ReactDOM from "react-do ...