异常介绍
异常体系
一定要能阐述异常的体系结构!
异常类的祖先类:Throwable
所有的异常都是一个类,如果不清楚可以在API帮助文档查询
运行时异常:编译时没有错误,运行时可能会出错,通常是代码不严谨导致的
编译时异常(不包含语法错误):主要起提醒作用,需要在运行之前给出解决方式
异常处理方式
异常的默认处理流程
异常处理方式的选择
方式1:try catch捕获异常
方式2:throws抛出异常
throw和throws的区别
Student.java
package com.EveX.domain;
public class Student {
String name;
int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) throws Exception {
if(age > 120 || age < 0) {
throw new Exception("输入年龄范围有误,请重新输入");
}
this.age = age;
}
public String toString() {
return "Student{name = " + name + ", age = " + age + "}";
}
}
ExceptionDemo.java
package com.EveX.exception.handle;
import com.EveX.domain.Student;
import java.util.Scanner;
public class ExceptionDemo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Student stu = new Student();
System.out.println("请输入姓名:");
String name = in.nextLine();
stu.setName(name);
System.out.println("请输入年龄:");
int age = 0;
while (true) {
try {
age = Integer.parseInt(in.nextLine());
stu.setAge(age);
break;
} catch (NumberFormatException e) {
System.out.println("NumberFormatException,请重新输入整数:");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
System.out.println(stu);
}
}
自定义异常
StudentAgeException.java
package com.EveX.exception;
public class StudentAgeException extends RuntimeException{
public StudentAgeException() {
}
public StudentAgeException(String message) {
super(message);
}
}
Student.java
package com.EveX.domain;
import com.EveX.exception.StudentAgeException;
public class Student {
String name;
int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
setAge(age);
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) throws StudentAgeException {
if(age > 120 || age < 0) {
throw new StudentAgeException("输入年龄范围有误,请重新输入");
}
this.age = age;
}
public String toString() {
return "Student{name = " + name + ", age = " + age + "}";
}
}
TryCatchDemo.java
package com.EveX.exception.handle;
import com.EveX.domain.Student;
import com.EveX.exception.StudentAgeException;
import java.util.Scanner;
public class TryCatchDemo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Student stu = new Student();
System.out.println("请输入姓名:");
String name = in.nextLine();
stu.setName(name);
System.out.println("请输入年龄:");
int age = 0;
while (true) {
try {
age = Integer.parseInt(in.nextLine());
stu.setAge(age);
break;
} catch (NumberFormatException e) {
System.out.println("NumberFormatException,请重新输入整数:");
} catch (StudentAgeException e) {
System.out.println(e.getMessage());
}
}
System.out.println(stu);
}
}