9异常
9.1异常处理机制一:try…catch
1、格式1:
try{
可能发生异常的代码
}catch(异常类型1 异常对象名){//异常对象名绝大多数都是写e
处理这个异常的代码
}catch(异常类型2 异常对象名){
处理这个异常的代码
}catch(异常类型3 异常对象名){
处理这个异常的代码
}... ...
格式2:
try{
可能发生异常的代码
}catch(异常类型1 异常对象名){//异常对象名绝大多数都是写e
处理这个异常的代码
}catch(异常类型2 异常对象名){
处理这个异常的代码
}catch(异常类型3 异常对象名){
处理这个异常的代码
}
... ...
finally{
不管try中是否发生异常,也不管catch是否可以捕获异常,这里代码都必须执行
}
2、异常对象的常用方法
(1)e.printStackTrace();
打印异常的详细信息,包括对象跟踪信息,即这个异常对象一路经过了哪些方法
(2)e.getMessage();
返回异常对象中简单的错误信息提示
import java.util.InputMismatchException;
import java.util.Scanner;
public class BugdDemo3 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("请输入2个整数:");
try {
int a=sc.nextInt();
int b =sc.nextInt();
System.out.println(a/b);
}
catch (InputMismatchException e) {
System.out.println("输入错误");
}
// catch (ArithmeticException e) {
// System.out.println("除数为0");
// }
finally {
System.out.println("其他错误");
}
}
}
import java.util.InputMismatchException;
import java.util.Scanner;
public class BugdDemo3 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("请输入2个整数:");
try {
int a=sc.nextInt();
int b =sc.nextInt();
System.out.println(a/b);
}
catch (InputMismatchException e) {
System.out.println("输入错误");
}
// catch (ArithmeticException e) {
// System.out.println("除数为0");
// }
finally {
System.out.println("其他错误");
}
}
}
9.2异常处理机制二:Throws&throw
throws关键字
异常处理的方式之二:
在当前方法中不处理,扔/抛给调用者处理(throws的作用)
格式:
【修饰符】 返回值类型 方法名(【形参列表】)throws 异常列表们{
}
说明:throws后面可以跟好几个异常,顺序无所谓,每一个异常之间使用,分割
throw和throws的区别:
1)thow
用在方法体内,跟的是异常对象名 => new
只能抛出一个异常对象名
throw是用来主动制造并抛出异常,由方法体内的语句处理
throw是抛出了异常,执行throw则一定抛出了某种异常
关键字 throw 后面,写异常类的对象=>new
2)throws
throws关键字用/写在方法的声明上,不是方法内部
throws后面跟的是异常类名,不是对象 => new
throws是声明方法可能会发生的异常,throws表示抛出异常,由该方法的调用者来处理,处理方式:调用者可以throws继续向上抛出或者在方法内部使用try...catch...
throws表示出现异常的一种可能性,并不一定会发生这些异常
注意:如果main方法里面单独开启了一条线程,并且调用了抛出异常的方法,则只能在方法内部进行try...catch处理,因为方法内部不能使用throws抛出异常类!
throws案例:
public class DemoBug {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("请输入2个整数:");
try {
int a=sc.nextInt();
int b =sc.nextInt();
chushu(a, b);
}
catch (InputMismatchException e) {
System.out.println("输入错误");
}
catch (ArithmeticException e) {
System.out.println("除数为0");
}
finally {
System.out.println("其他错误");
}
}
public static void chushu(int a,int b) throws InputMismatchException,ArithmeticException{
System.out.println(a/b);
}
}
throw案例:
public class ExceptionDemo {
public static void main(String[] args) {
String s="a.1txt";
method(s);
}
public static void method(String s) {
if(!s.endsWith(".txt")) {
throw new NullPointerException();//故意创建异常对象,用thow说明此处有异常-空指针异常
}
System.out.println("我要继续");
}
}
异常综合案例:
需求:
创建一个Student类,两个属性:name(string)、age(int)
构造方法:初始化name和age
方法:setname()、setage()、getname()、getage()
要抛出异常:
1)若name为null空,则抛出异常,提示信息“name null”
2)若age>120,<0,则抛出异常,提示信息“age error”
(创建StudentException异常类,构造方法:初始化出错信息)
异常测试类:EceptionTest类
输入形式:
第一个数据name,字符串, 有可能数值
第二个数age:int,有可能输入字符串
public class Student {
private String name;
private int age;
public Student(String name, int age) throws StudentException{
if(name == null || name == " " || name.isEmpty()){
throw new StudentException("name null");
}else{
this.name = name;
}
if(age < 0 || age > 120){
throw new StudentException("age error");
}else{
this.age = age;
}
}
public void setAge(int age) throws StudentException{
if(age < 0 || age > 120){
throw new StudentException("update "+"age error");
}else {
this.age = age;
}
}
public int getAge(){
return age;
}
public String getName(){
return name;
}
}
public class StudentException extends Exception {
String msg;
public StudentException(String msg){
this.msg = msg;
}
public String getMessage(){
return msg;
}
}
import java.util.Scanner;
import java.util.InputMismatchException;
public class ExceptionTest2 {
// private static Scanner reader;
public static void main(String[] args) {
int age;
String name;
// Student stu = null;
try{
Scanner reader = new Scanner(System.in);
name = reader.nextLine();
age = reader.nextInt();
Student stu = new Student(name, age);
System.out.println("student:" + stu.getName() + "," + stu.getAge());
// age = reader.nextInt();
// stu.setAge(age);
// System.out.println("student_set:" + stu.getName() + "," + stu.getAge());
}catch(InputMismatchException e){
System.out.println("format error");
}catch(StudentException e){
System.out.println(e.getMessage());
}
}
}