目录
异常概念
异常分类
检查异常也称为编译期异常
ParseException 解析异常
运行时异常
ArithmeticException 算术异常
ArrayIndexOutOfBoundsException数组下标越界异常
ClassCastException类型转换异常
异常处理方式
使用try、catch、finally捕获异常
使用throws声明抛出异常
自定义异常
继承自Exception的自定义异常
继承自RuntimeException的自定义异常
作业
异常概念
什么是异常(Exception)
异常是程序在编译或运行过程出现的例外,这些例外在有的可以避免有的却无法避免
异常分类
根据异常的出现时期主要分为编译时异常和运行时异常两大类。分别对应在编写代码时时候和运行代码的时候。
检查异常也称为编译期异常
不可避免 必须进行异常处理,要不编译器报错Exception以及它的子类(除去RuntimeException)
常见的编译期异常
ParseException 解析异常
import java.text.SimpleDateFormat;
import java.util.Date;
public class ParseException {
public static void main(String[] args) {
//根据指定模式创建SimpleDateFormat对象
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
try {
//将字符串日期按照yyyy年MM月dd日的模式解析为Date对象
Date date=sdf.parse("2022-01-01");
//输入解析的时间对象
System.out.println(date);
} catch (java.text.ParseException e) {
//打印出解析字符串时间的异常
e.printStackTrace();
}
}
}
当字符串不是以yyyy年MM月dd日的模式写的时候,就会出现解析异常
运行时异常
代码程序在运行的过程中肯能出现的异常,编译器在编译代码时,不能检测出来。
常见的运行时异常有
NullPointerException 空指针异常
代码示例
public class NullPointerException {
public static void main(String[] args) {
String name=null;
System.out.println(name.hashCode());
}
}
这样写代码的时候,对象为null的时候,去调用对象的方法和属性,运行程序就会出现空指针异常
ArithmeticException 算术异常
import java.util.Scanner;
public class ArithmeticException {
public static void main(String[] args) {
//创建Scanner对象
Scanner sc=new Scanner(System.in);
//通过 Scanner 类来获取用户的输入
int i=sc.nextInt();
//输出10除以用户输入值的结果
System.out.println(10/i);
}
}
当用户输入0的时候,就会抛出算数异常。
ArrayIndexOutOfBoundsException数组下标越界异常
public class ArrayIndexOutOfBoundsException {
public static void main(String[] args) {
int[] nums=new int[3];
System.out.println(nums[3]);
}
}
创建一个只有3个元素的数组,数组的下标是从0开始的,也就是最后一个下标是2,当输入出超过2的下标的数组时候,就会出现数组下标越界异常。
ClassCastException类型转换异常
public class ClassCastException {
public static void main(String[] args) {
Object i=new Integer(10);
String str=(String)i;
System.out.println(str);
}
}
Object是所有类的父类,父类转子类可以通过强行转化实现。但是Obejct类的参数赋值的是一个Integer类的对象,当它强行转化为文本的时候,就会抛出类型转换异常
异常处理方式
Java编译期异常必须要进行处理,否则Java源文件在编译时编译器会提示错误,且源文件无法成功编译
Java编译期异常的处理方式有两种
使用try、catch、finally关键字捕获异常使用throws关键字声明抛出异常
使用try、catch、finally捕获异常
try{
//可能会出现异常的代码
}catch(ParseException e){
//捕获执行的代码
}finally{
//不管是否发生异常都要执行的代码
}
finally根据情况可以省略
import java.text.SimpleDateFormat;
import java.util.Date;
public class ParseException {
public static void main(String[] args) {
//根据指定模式创建SimpleDateFormat对象
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
try {
//将字符串日期按照yyyy年MM月dd日的模式解析为Date对象
Date date=sdf.parse("2022-01-01");
} catch (java.text.ParseException e) {
System.out.println("字符串不符合解析模式");
}
}
}
使用throws声明抛出异常
import java.text.SimpleDateFormat;
import java.util.Date;
public class ParseException {
public static void main(String[] args) throws java.text.ParseException {
//根据指定模式创建SimpleDateFormat对象
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
//将字符串日期按照yyyy年MM月dd日的模式解析为Date对象
Date date=sdf.parse("2022-01-01");
}
}
自定义异常
自定义异常
在开发中,可能需要自定义异常类
自定义异常根据继承的父类不同分为两类
继承自Exception的自定义异常继承自RuntimeException的自定义异常
继承自Exception的自定义异常为编译期异常必须要进行处理
继承自RuntimeException的自定义异常为运行时异常不需要进行特别的处理。
继承自Exception的自定义异常
//自定义字符串空格异常
public class StringBlankException extends Exception{
public StringBlankException() {
System.out.println("字符串有空格");
}
public StringBlankException(String message) {
super(message);
}
}
public class StringBlankExceptionTest {
public static void main(String[] args) throws StringBlankException {
String str="你好,我是李焕英! ";
if(str.contains(" ")){
throw new StringBlankException();
}
}
}
继承自RuntimeException的自定义异常
//自定义字符串空格异常
public class StringBlankException extends RuntimeException{
public StringBlankException() {
System.out.println("字符串有空格");
}
public StringBlankException(String message) {
super(message);
}
}
public class StringBlankExceptionTest {
public static void main(String[] args){
String str="你好,我是李焕英! ";
if(str.contains(" ")){
throw new StringBlankException();
}
}
}
作业
//自定义字符串空格异常
public class StringBlankException extends RuntimeException{
public StringBlankException() {
System.out.println("字符串有空格");
}
public StringBlankException(String message) {
super(message);
}
}
public class StringBlankExceptionTest {
public static void main(String[] args){
String str="你好,我是李焕英! ";
if(str.contains(" ")){
throw new StringBlankException();
}
}
}
作业
作业一
自定义一个学生类,属性有 姓名 年龄,如果用户在给学生年龄赋值时,年龄小于0抛出一个AgeLT0Exception,大于150 抛出一个AgeGT150Exception



