目录
1. 练习
package com.msb01;
import java.util.Scanner;
/**
* @Auther: jack.chen
* @Date: 2023/9/17 - 09 - 17 - 10:58
* @Description: com.msb01
* @version: 1.0
*/
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数:");
int num1 = sc.nextInt();
System.out.println("请输入第二个数:");
int num2 = sc.nextInt();
System.out.println(num1/num2);
}
}
通过 if-else堵住异常 可读性 差
2. 异常三联 try-catch-finally
package com.msb01;
import java.util.Scanner;
/**
* @Auther: jack.chen
* @Date: 2023/9/17 - 09 - 17 - 10:58
* @Description: com.msb01
* @version: 1.0
*/
public class Test {
public static void main(String[] args) {
try {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数:");
int num1 = sc.nextInt();
System.out.println("请输入第二个数:");
int num2 = sc.nextInt();
System.out.println(num1/num2);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(1111);
System.out.println(222);
System.out.println(333);
}
}
异常被打印 后续代码正常执行
如果catch中异常的类型不匹配 不会走catch的逻辑 相当于 没捕获
异常捕获后如何处理
- 不处理
- 自定义输出
- 打印异常
- 抛出异常
![](/i/l/?n=23&i=blog/1422290/202309/1422290-20230917110918461-828857373.png)
什么情况下 try-catch 后面的逻辑(谢谢使用计算器)不会执行
- throw抛出异常
- catch 没有捕获住异常
- try中遇到return 方法里面将返回值返回调用方
怎样才能 无论如何都执行try-catch 后面的逻辑
将逻辑放入 finally
reuturn与finally的先后顺序
先finally后return (记忆方法 f排序在r的前面)
哪些逻辑会放入finally
关系数据源 关闭io 关闭socket
有一句代码很厉害可以不让finally里面的逻辑执行
System.exit(0); jvm虚拟机退出
package com.msb01;
import java.util.Scanner;
/**
* @Auther: jack.chen
* @Date: 2023/9/17 - 09 - 17 - 10:58
* @Description: com.msb01
* @version: 1.0
*/
public class Test {
public static void main(String[] args) {
try {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数:");
int num1 = sc.nextInt();
System.out.println("请输入第二个数:");
int num2 = sc.nextInt();
System.out.println(num1/num2);
// System.exit(0);
return;
} catch (Exception e) {
System.out.println("对不起输出有误 无法计算");
System.out.println(e.toString());
System.out.println(e.getMessage());
e.printStackTrace();
// throw e;
} finally {
System.out.println("谢谢使用计算器");
}
}
}
并列符| 连接多个异常
package com.msb01;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* @Auther: jack.chen
* @Date: 2023/9/17 - 09 - 17 - 10:58
* @Description: com.msb01
* @version: 1.0
*/
public class Test {
public static void main(String[] args) {
try {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数:");
int num1 = sc.nextInt();
System.out.println("请输入第二个数:");
int num2 = sc.nextInt();
System.out.println(num1/num2);
// System.exit(0);
return;
} catch (ArithmeticException |InputMismatchException |Exception) {
System.out.println("对不起输出有误 无法计算");
} finally {
System.out.println("谢谢使用计算器");
}
}
}
3.异常的分类
检查异常:防范于未然 写代码的时候就要对代码的异常进行处理
运行异常:写代码的会后不用特意对异常进行处理 运行过程中没法执行 才会抛出异常
package com.msb01;
/**
* @Auther: jack.chen
* @Date: 2023/9/17 - 09 - 17 - 11:38
* @Description: com.msb01
* @version: 1.0
*/
public class TestException01 {
public static void main(String[] args) {
int[] a1 = {1, 2,3};
System.out.println(a1[10]);
}
}
public class TestException01 {
public static void main(String[] args) {
int[] a1 = {1, 2,3};
// System.out.println(a1[10]);
int[] a2 = null;
System.out.println(a2.length);
}
}
package com.msb01;
/**
* @Auther: jack.chen
* @Date: 2023/9/17 - 09 - 17 - 11:41
* @Description: com.msb01
* @version: 1.0
*/
public class TestException02 {
public static void main(String[] args) {
Class.forName("com.msb01.Test").newInstance();
}
}
![](/i/l/?n=23&i=blog/1422290/202309/1422290-20230917114307186-307719948.png)
package com.msb01;
/**
* @Auther: jack.chen
* @Date: 2023/9/17 - 09 - 17 - 11:41
* @Description: com.msb01
* @version: 1.0
*/
public class TestException02 {
public static void main(String[] args) {
try {
Class.forName("com.msb01.Test").newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
package com.msb01;
/**
* @Auther: jack.chen
* @Date: 2023/9/17 - 09 - 17 - 11:41
* @Description: com.msb01
* @version: 1.0
*/
public class TestException02 {
public static void main(String[] args) {
try {
Class.forName("com.msb01.Test").newInstance();
} catch (InstantiationException|IllegalAccessException|ClassNotFoundException e) {
e.printStackTrace();
}
}
}
alt+enter快捷键
提示两种处理方式 一个是 throws 一个是 try/catch
package com.msb01;
/**
* @Auther: jack.chen
* @Date: 2023/9/17 - 09 - 17 - 11:41
* @Description: com.msb01
* @version: 1.0
*/
public class TestException02 {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
Class.forName("com.msb01.Test").newInstance();
}
}
3. 子类throws的异常 小于等于父类的异常
package com.msb01;
/**
* @Auther: jack.chen
* @Date: 2023/9/17 - 09 - 17 - 11:47
* @Description: com.msb01
* @version: 1.0
*/
public class Person {
public void eat() throws RuntimeException{
System.out.println("eat");
}
}
4.自定义异常
package com.msb01;
/**
* @Auther: jack.chen
* @Date: 2023/9/17 - 09 - 17 - 11:51
* @Description: com.msb01
* @version: 1.0
*/
public class MyException extends RuntimeException {
static final long serialVersionUID = -70348971907L;
public MyException() {
}
public MyException(String message) {
super(message);
}
}
检查异常 需要使用try-catch捕获 或者throws
标签:Exception,java,msb01,System,day10,println,com,public,out From: https://www.cnblogs.com/cavalier-chen/p/17708189.html