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

多个异常使用捕获又该如何处理呢?
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. 09. 树莓派ASP.NET环境配置

    在树莓派上部署ASP.NET环境(树莓派做ASP.NET项目服务器),之后Windows上开发的ASP.NET项目可以部署在树莓派上.配置过程如下: 前言:本篇文章内容是根据mono官网上查阅的配置教 ...

  2. 带你了解极具弹性的Spark架构的原理

    摘要:相比MapReduce僵化的Map与Reduce分阶段计算相比,Spark的计算框架更加富有弹性和灵活性,运行性能更佳. 本文分享自华为云社区<Spark架构原理>,作者:JavaE ...

  3. Vue3 setup详解

    setup执行的时机 在beforeCreate之前执行(一次),此时组件对象还没创建: this是undefined,不能通过this来访问data/computed/methods/props: ...

  4. 攻防世界-MISC:掀桌子

    这是攻防世界新手练习区的第八题,题目如下: 就给了一串16进制的字符串.哎,又是不懂,看了一下官方WP,说是将每两位16进制数转换为10进制,再减去128再转换为ASCII码.直接上脚本 str1 = ...

  5. 一次不规范HTTP请求引发的nginx响应400问题分析与解决

    背景 最近分析数据偶然发现nginx log中有一批用户所有的HTTP POST log上报请求均返回400,没有任何200成功记录,由于只占整体请求的不到0.5%,所以之前也一直没有触发监控报警,而 ...

  6. 一文详解 Ansible 自动化运维

    开源Linux 一个执着于技术的公众号 一.Ansible 概述 Ansible 是近年来越来越火的一款开源运维自动化工具,通过Ansible可以实现运维自动化,提高运维工程师的工作效率,减少人为失误 ...

  7. 使用NFS作为Glance存储后端

    NFS服务介绍 NFS网络文件系统提供了一种在类UNIX系统上共享文件的方法.目前NFS有3个版本:NFSv2.NFSv3.NFSv4.CentOS7默认使用NFSv4提供服务,优点是提供了有状态的连 ...

  8. 【面试普通人VS高手系列】说一说Mybatis里面的缓存机制

    一个工作了 5年的程序员,在私信里面不断向我诉苦. 他说,他用了Mybatis这么久,怎么滴也算是精通Mybatis了吧. 结果竟然在Mybatis这个面试题上翻车了! 真的好烦! 好吧,我们今天来看 ...

  9. 如何定制.NET6.0的日志记录

    在本章中,也就是整个系列的第一部分将介绍如何定制日志记录.默认日志记录仅写入控制台或调试窗口,这在大多数情况下都很好,但有时需要写入到文件或数据库,或者,您可能希望扩展日志记录的其他信息.在这些情况下 ...

  10. linux篇-tomcat:Cannot find /usr/local/tomcat1/bin/setclasspath.sh

    首先看下报错代码: Cannot find /usr/local/tomcat1/bin/setclasspath.sh This file is needed to run this program ...