Java之异常处理
1.异常处理
关键词 | 作用 | 备注 |
---|---|---|
try | 异常发现区间 | 执行代码逻辑,在执行期间发掘是否有异常 |
catch | 捕获异常后处理区间 | 若try区间有异常,捕获异常在此区间处理 |
finally | 总是执行 | 不论是否有异常 此区间代码总是执行 释放占用资源 如:IO流 |
throw | 抛出方法中异常 | 异常扔到方法定义上(甩锅给上级) |
throws | 抛出方法定义上异常 | 异常扔到调用方法的地方(甩锅给上级) |
package com.exception;
// 异常处理五个关键词
// try 异常发现区间
// catch 异常处理区间 若try区间有异常,捕获异常在此区间处理
// finally 不论是否有异常 此区间代码总是执行 释放占用资源 如:IO流
// throw 抛出异常 方法中 异常扔到方法定义上(甩锅给上级)
// throws 抛出异常 在方法定义上 异常扔到调用方法的地方(甩锅给上级)
public class Test01 {
public static void main(String[] args) {
// finally会阻止try catch 的return
// System.out.println(new Test01().e2()); // 2
System.out.println(new Test01().e3()); // 2
}
public void exceptions (int a,int b) throws ArithmeticException{// 此处异常 传递给方法调用第28行 new Test01().exceptions(1,0);
if(b==0){
throw new ArithmeticException();// 此处异常 传递给方法定义第18行 public void exceptions (int a,int b) throws ArithmeticException
}else {
System.out.println("666");
}
}
public void e1(){
try { // 异常发现区间
new Test01().exceptions(1,0);
} catch (ArithmeticException e) { // 捕获异常后处理区间
e.printStackTrace();
System.out.println("catch");
}finally { // 总是执行
System.out.println("始终执行");
}
}
// 测试finally会阻止try catch 的return
public int e2(){
try {
new Test01().exceptions(1,0);
return 0;
} catch (ArithmeticException e) {
e.printStackTrace();
System.out.println("catch");
return 1;
}finally {
System.out.println("始终执行");
return 2;
}
}
// 测试finally会阻止try catch 的return
public int e3(){
try {
new Test01().exceptions(1,1);
return 0;
} catch (ArithmeticException e) {
e.printStackTrace();
System.out.println("catch");
return 1;
}finally {
System.out.println("始终执行");
return 2;
}
}
}
2.自定义异常处理
MyException类为自定义的异常处理类,Test02类调用异常类
自定义异常类必须继承Exception类
MyException类
package com.exception;
// 自定义异常类必须继承Exception类
public class MyException extends Exception{
private int num;
// MyException构造方法
public MyException(int num) {
// 初始化num赋值
this.num = num;
}
// 最终异常是什么
@Override
public String toString() {
return "MyException"+this.num;
}
}
Test02 类
package com.exception;
public class Test02 {
static void e1(int a) throws MyException{ // 此处异常 传递给方法调用第18行 e1(10);
System.out.println("传入参数:"+a);
if(a<10){
System.out.println("正常执行");
}else {
throw new MyException(a); //此处异常 传递给方法定义第4行 static void e1(int a) throws MyException{
}
System.out.println("结束方法");
}
public static void main(String[] args) {
try {
e1(10);
} catch (MyException e) {
System.out.println("可以添加逻辑处理");
System.out.println(e); // 打印为MyException类中toString()方法
}
// 执行后打印
// 传入参数:10
// 可以添加逻辑处理
// MyException10
}
}
3.扩展之static
static为静态修饰符,与类一起加载。使用static修饰的方法或者属性,无需实例化, 可以使用类名直接调用。
探索父类 子类 初始化时 静态函数块 、 匿名函数 、 构造器的运行顺序
Person父类
package com.oop.demo06;
public class Person {
private String name;
static {
System.out.println("父类静态函数块");
}
{
System.out.println("父类匿名函数块");
}
public Person(){
System.out.println("父类无参构造函数");
}
public Person(String name){
this.name = name;
}
}
Student类为子类
package com.oop.demo06;
// 静态导入类
import static java.lang.Math.random;
public class Student extends Person {
// 程序运行时只执行一次
static {
System.out.println("子类静态函数块");
}
{
System.out.println("子类匿名函数块");
}
public Student(){
System.out.println("子类无参构造函数");
}
public static void main(String[] args) {
Student student = new Student();
// 静态导入类
System.out.println(random());
}
}
// 父类静态函数块
// 子类静态函数块
// 父类匿名函数块
// 父类无参构造函数
// 子类匿名函数块
// 子类无参构造函数