今天主要学习了几种异常
基本概念
异常就是"不正常"的含义,在ava语言中体现为运行阶段发生的错误。java.lang.Throwable类是ava语言中所有错误(Error类)和异常(Exception类)的超类。其中Error类主要用于描述比较严重无法编码解决的错误,如:JVM挂了等。其中Exception类主要用于描述比较轻微可以编码解决的错误,如:O作为除数等。
基本分类
java.lang.Exception类的所有子类主要分为两大类:
RuntimeException类-运行时异常,也叫做非检测性异常。
IO Exception和其它异常-其它异常,也叫做检测性异常。-所谓检测性异常就是指在编译阶段能够被编译器检测出来的异常。
异常的捕获
(1)语法格式
try{
编写所有可能发生异常的语句:
}
catch(异常类型变量名){
编写针对该类异常进行处理的语句;
} finally {
编写无论是否发生异常都需要处理的语句;
}
(2)catch
catch(Exception e){
} finally {
主要用于善后处理,如:关闭已经打开的文件、删除临时创建的文件等
}
(3)执行流程
try{
a;
b;
c;
} catch {
e
} finally{
f
}
当程序执行过程中没有发生异常时的执行流程:abce:
当程序执行过程中发生异常后的执行流程:abde:
异常的抛出
(1)基本概念
在某些特殊情况下异常不便于处理或无法处理时,就可以将异常转移给该方法的调用者,这个过程就叫做异常的抛出。
(2)语法格式
访问权限返回值类型方法名(形参列表)throws异常类 {
方法体:
}
(3)方法重写的原则
a.要求方法名相同、参数列表相同、返回值类型相同,从jk1.5开始允许返回子类类型:
b.要求访问权限不能变小,可以相同或变大:
c.要求不能抛出更大的异常
自定义异常
基本概念
虽然java官方提供了大量的异常类,但没有提供针对年龄不合理的异常,此时若希望描述这样的异常信息,就需要程序员自定义异常类。
实现流程
a.自定义xxxException继承自Exception或者其子类。
b.提供两个版本的构造方法,一个是无参构造方法,另外一
个是字符串作为参数的构造方法。
package ExceptionTest0130;
public class Person {
private String name;
private int age;
public Person() {
}
//重写toString
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
//get和set方法
public Person(String name, int age) throws AgeException {
this.name = name;
setAge(age);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
//扔自定义异常
public void setAge(int age) throws AgeException{
if(age > 0 && age <150) {
this.age = age;
} else {
throw new AgeException();
}
}
}
package ExceptionTest0130;
//自定义异常
public class AgeException extends Exception{
public AgeException() {
}
public AgeException(String message) {
super(message);
}
}
package ExceptionTest0130;
public class ExceptionAgeTest {
public static void main(String[] args) {
Person person = null;
try {
person = new Person("xiaoliu", 10);
} catch (AgeException e) {
e.printStackTrace();
}
System.out.println(person);
}
}
//没有异常的测试
Person{name='xiaoliu', age=10}
Process finished with exit code 0
//有异常的测试
ExceptionTest0130.AgeException
at ExceptionTest0130.Person.setAge(Person.java:39)
at ExceptionTest0130.Person.<init>(Person.java:20)
at ExceptionTest0130.ExceptionAgeTest.main(ExceptionAgeTest.java:8)
null
Process finished with exit code 0
以下是几种常见的异常
package Exception01;
import org.junit.Test;
import java.io.IOException;
public class ExceptionTest {
@Test
public void exceptionTest() {
//ArithmeticException
int a = 10;
int b = 0;
if(b != 0) {
System.out.println((a / b));
}
//ArrayIndexOutOfBoundsException
int[] arr = new int[5];
int len = 5;
if(len < 5) {
System.out.println(arr[5]);
}
//NullPointerException
String str = null;
if (str != null ) {
System.out.println(str.length());
}
//ClassCastException
Exception exception = new Exception();
if(exception instanceof IOException) {
IOException io = (IOException) exception;
System.out.println(io);
}
//NumberFormatException
String str2 = "123aa";
//匹配正则表达式 \d+表示数字
if (str2.matches("\\d+")){
System.out.println(Integer.parseInt(str2));
}
}
}
ok完事