异常-try...catch的方式处理异常2
2024-09-05 07:18:09
package cn.itcast_02; /*
* A:一个异常
* B:二个异常的处理
* a:每一个写一个try...catch
* b:写一个try,多个catch
* try{
* ...
* }catch(异常类名 变量名) {
* ...
* }
* catch(异常类名 变量名) {
* ...
* }
* ...
*
* 注意事项:
* 1:能明确的尽量明确,不要用大的来处理。
* 2:平级关系的异常谁前谁后无所谓,如果出现了子父关系,父必须在后面。
*
* 注意:
* 一旦try里面出了问题,就会在这里把问题给抛出去,然后和catch里面的问题进行匹配,
* 一旦有匹配的,就执行catch里面的处理,然后结束了try...catch,继续执行后面的语句。
*/
public class ExceptionDemo2 {
public static void main(String[] args) { // method1(); // method2(); // method3(); method4();
} public static void method4() {
int a = 10;
int b = 0;
int[] arr = { 1, 2, 3 }; // 爷爷在最后
try {
System.out.println(a / b);
System.out.println(arr[3]);
System.out.println("这里出现了一个异常,你不太清楚是谁,该怎么办呢?");
} catch (ArithmeticException e) {
System.out.println("除数不能为0");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("你访问了不该的访问的索引");
} catch (Exception e) {//它可以匹配所有的异常
System.out.println("出问题了");
} System.out.println("over"); /*
try {
System.out.println(a / b);
System.out.println(arr[3]);
System.out.println("这里出现了一个异常,你不太清楚是谁,该怎么办呢?");
} catch (Exception e) {//爷爷在前面是不可以的
System.out.println("出问题了");
} catch (ArithmeticException e) {
System.out.println("除数不能为0");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("你访问了不该的访问的索引");
} System.out.println("over");*/
} // 两个异常的处理,写一个try,多个catch
public static void method3() {
int a = 10;
int b = 0;
int[] arr = { 1, 2, 3 }; try {
System.out.println(arr[3]);
System.out.println(a / b);
// System.out.println(arr[3]);
} catch (ArithmeticException e) {
System.out.println("除数不能为0");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("你访问了不该的访问的索引");
} System.out.println("over");
} // 两个异常的处理,每一个写一个try...catch
public static void method2() {
int a = 10;
int b = 0;
try {
System.out.println(a / b);
} catch (ArithmeticException e) {
System.out.println("除数不能为0");
} int[] arr = { 1, 2, 3 };
try {
System.out.println(arr[3]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("你访问了不该的访问的索引");
} System.out.println("over");
} // 一个异常的处理
public static void method1() {
// 第一阶段
int a = 10;
// int b = 2;
int b = 0; try {
System.out.println(a / b);
} catch (ArithmeticException ae) {
System.out.println("除数不能为0");
} // 第二阶段
System.out.println("over");
}
}
最新文章
- CORS简介
- runtime运行时
- 将时间转换为xxx天前 xxx..前
- Hadoop阅读笔记(四)——一幅图看透MapReduce机制
- Android 自定义view中的属性,命名空间,以及tools标签
- [转载]C#.NET中Dns类的常用方法及说明
- default 关键字泛型代码中的默认关键字(C# 编程指南)
- Sicily 2005.Lovely Number
- SOA架构有基本的要求
- JAVA基础第一组(前5道题)
- vue2.0 路由模式mode=";history";的作用
- SharePoint 部件通过EditorPart自定义属性面板
- Spring Boot 2.0 教程 - 配置详解
- Java中的String,StringBuilder,StringBuffer
- 第六章Django
- 吴恩达课后作业学习1-week4-homework-multi-hidden-layer -2
- Node_初步了解(4)小爬虫
- Linux系统命令 3
- c 链表和动态内存分配
- (面试题)html中创建模态窗口的方法有哪些?
热门文章
- pm2 配合log4js处理日志
- 2017-12-3 Crontab(字符串处理)
- 一百零六:CMS系统之修改邮箱功能完成
- Django View视图
- leetcode 257. 二叉树的所有路径 包含(二叉树的先序遍历、中序遍历、后序遍历)
- 【VS开发】【编程开发】【C/C++开发】结构体中的数组与指针的内存分配情况说明
- 用Blackbox Exporter to Monitor web和端口
- python实用小功能
- memcached命令行、Memcached数据导出和导入
- XOR Guessing(交互题+思维)Educational Codeforces Round 71 (Rated for Div. 2)