Java异常
2021-04-20 本文已影响0人
媛猿YY
异常处理
![](https://img.haomeiwen.com/i13909229/22e858051bd0ec66.png)
- try-catch-finally
try块后面可以接0个或多个catch块,如果没有catch块,则必须跟一个finally块
package com.example.javabase.gongju.exception;
import java.util.Scanner;
public class TryDemoOne {
public static void main(String[] args) {
//要求:定义两个整数,接收用户的键盘输入,输出两数之商
Scanner input = new Scanner(System.in);
System.out.println("====运算开始====");
try {
System.out.print("前输入第一个整数");
int one = input.nextInt();
System.out.print("前输入第一个整数");
int two = input.nextInt();
// 被除数为0、非数字时会异常
System.out.println("one和two的⤴商是:" + (one / two));
} catch (Exception e) {
System.out.println("程序出错");
e.printStackTrace();
} finally {
System.out.println("====运算结束====");
}
}
}
多重catch
package com.example.javabase.gongju.exception;
import java.util.InputMismatchException;
import java.util.Scanner;
public class TryDemoOne {
public static void main(String[] args) {
//要求:定义两个整数,接收用户的键盘输入,输出两数之商
/*
* ArithmeticException 算数异常
* InputMismatchException 格式异常
* */
Scanner input = new Scanner(System.in);
System.out.println("====运算开始====");
try {
System.out.print("前输入第一个整数");
int one = input.nextInt();
System.out.print("前输入第一个整数");
int two = input.nextInt();
// 被除数为0、非数字时会异常
System.out.println("one和two的商是:" + (one / two));
} catch (ArithmeticException e) {
System.out.println("程序不允许为0");
e.printStackTrace();
} catch (InputMismatchException e) {
System.out.println("请输入整数");
e.printStackTrace();
} catch (Exception e) {
System.out.println("出错了");
e.printStackTrace();
} finally {
System.out.println("====运算结束====");
}
}
}
- 终止finally代码块运行
System.exit(1);//终止程序运行
- finally代码块不能写return
public static int test() {
Scanner input = new Scanner(System.in);
System.out.println("====运算开始====");
try {
System.out.print("前输入第一个整数");
int one = input.nextInt();
System.out.print("前输入第一个整数");
int two = input.nextInt();
return one / two;
} catch (ArithmeticException e) {
System.out.println("除数不允许为0");
return 0;
} finally {
System.out.println("====运算结束====");
// return -100000; 不支持
}
}
throws
- 如果一个方法可能会出现异常,但没有能力处理这种异常,可以在方法声明处用throws子句来声明抛出异常
-
throws语句用在方法定义时声明该方法要抛出的异常类型
image.png
- 当方法抛出异常列表中的异常时,方法将不对这些类型及其子类类型的异常做处理,该抛向调用该方法的方法,由它处理
package com.example.javabase.gongju.exception;
import java.util.InputMismatchException;
import java.util.Scanner;
public class TryDemoThree {
public static void main(String[] args) {
// try {
// int result = test();
// System.out.println("one和two的商是:" + result);
// } catch (ArithmeticException e) {
// System.out.println("除数不允许为0");
// e.printStackTrace();
// }catch (InputMismatchException e){
// System.out.println("类型错误,请输入整数");
// e.printStackTrace();
// }
try {
int result = test();
System.out.println("one和two的商是:" + result);
} catch (Exception e) {
}
}
/*
* 通过throws 抛出异常时,针对啃出现的多种异常,解决方案
* 1、throws后面接多个异常类型,中间用逗号分隔
* 2、throws后面接Exception
* */
// public static int test() throws ArithmeticException, InputMismatchException {
// Scanner input = new Scanner(System.in);
// System.out.println("====运算开始====");
// System.out.print("前输入第一个整数");
// int one = input.nextInt();
// System.out.print("前输入第一个整数");
// int two = input.nextInt();
// System.out.println("====运算结束====");
// return one / two;
//
// }
public static int test() throws Exception {
Scanner input = new Scanner(System.in);
System.out.println("====运算开始====");
System.out.print("前输入第一个整数");
int one = input.nextInt();
System.out.print("前输入第一个整数");
int two = input.nextInt();
System.out.println("====运算结束====");
return one / two;
}
}
throw
- throw用来抛出一个异常
throw new IOException();
- throw抛出的只能是可抛出类Throwable或者其子类的实例对象
- 通过try..catch包含throw语句,自己抛出自己处理
public static void main(String[] args) {
testAge();
}
public static void testAge() {
try {
System.out.println("请输入年龄:");
Scanner input = new Scanner(System.in);
int age = input.nextInt();
if (age < 18 || age > 80) {
throw new Exception("必须有亲友陪同");
} else {
System.out.println("欢迎入住");
}
} catch (Exception e) {
e.printStackTrace();
}
}
- 通过throws在方法声明处抛出异常类型--谁用谁处理--调用者可以自己处理
public static void main(String[] args) {
try {
testAge();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testAge() throws Exception {
System.out.println("请输入年龄:");
Scanner input = new Scanner(System.in);
int age = input.nextInt();
if (age < 18 || age > 80) {
throw new Exception("必须有亲友陪同");
} else {
System.out.println("欢迎入住");
}
}
自定义异常
就是定义一个类,去继承Throwable类或者它的子类
自定义类 HotelAgeExecption.java
package com.example.javabase.gongju.exception;
public class HotelAgeExecption extends Exception {
public HotelAgeExecption(){
super("必须有亲友陪同");
}
}
调用
public static void main(String[] args) {
try {
testAge();
} catch (HotelAgeExecption e) {
System.out.println(e.getMessage());
System.out.println("酒店前台工作人员不允许办理入住");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testAge() throws HotelAgeExecption {
System.out.println("请输入年龄:");
Scanner input = new Scanner(System.in);
int age = input.nextInt();
if (age < 18 || age > 80) {
// throw new Exception("必须有亲友陪同");
throw new HotelAgeExecption();
} else {
System.out.println("欢迎入住");
}
}
异常链
- 有时候我们会捕获一个异常后再抛出一个异常
- 将异常发生的原因一个传一个串起来,即把底层的异常信息传给上层,这样逐层抛出
package com.example.javabase.gongju.exception;
public class TryDemoFive {
public static void main(String[] args){
try {
testThree();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testOne() throws HotelAgeExecption{
throw new HotelAgeExecption();
}
public static void testTwo() throws Exception {
try {
testOne();
} catch (HotelAgeExecption e) {
throw new Exception("我是新产生的异常1",e);
}
}
public static void testThree() throws Exception {
try {
testTwo();
} catch (Exception e) {
throw new Exception("我是新产生的异常2",e);
}
}
}