java基础知识21 异常处理try与throw的相互处理e.getcause
一 异常结构
1.1 层次结构
异常的层次结构 * 异常的层次结构可以通过连续调用getCause()方法来获取。例如,如果有一个异常A,它是由异常B引起的,而异常B又是由异常C引起的,那么可以通过以下方式获取到异常C:
Throwable rootCause = e.getCause().getCause();
e 指当前A;
e.getCause 指的B;
e.getCause().getCause() 指的C;
1.2 案例演示1 相互抛出
1.2.1 BeijingException异常
public class BeijingException extends RuntimeException{public BeijingException(String message) {super(message);}public BeijingException(String message, Throwable cause) {super(message, cause);}
}
1.2.2 ZhengzhouException异常
public class ZhengzhouException extends RuntimeException{public ZhengzhouException(String message) {super(message);}public ZhengzhouException(String message, Throwable cause) {super(message, cause);}
}
1.2.3 模拟调用
1.异常相互传递向上抛出。
package com.ljf.springboot.mybaits.demos.exdemo;/*** @ClassName: Test* @Description: TODO* @Author: admin* @Date: 2024/11/01 10:33:56 * @Version: V1.0**/
public class Test {public static void main(String[] args) {try {aInfo();} catch (Throwable e) {System.out.println("e:"+e);System.out.println("e1:"+e.getCause());System.out.println("e2:"+e.getCause().getCause());System.out.println("e3:"+e.getCause().getCause().getCause());}}public static void aInfo() {try {bInfo();} catch (ZhengzhouException e) {// throw new BeijingException("BException thrown due to AException");throw new BeijingException("beijing",e);}//https://blog.csdn.net/CSDN_KONGlX/article/details/125486607}public static void bInfo() {// throw new ZhengzhouException("郑州内部异常 throw");try {cInfo();} catch (NullPointerException e) {// throw new ZhengzhouException("郑州内部异常 throw");throw new ZhengzhouException("郑州内部异常 throw",e);}}public static void cInfo(){throw new NullPointerException("内部空指针异常...");}
}
执行结果:
1.3 案例演示2 拦截异常封装后抛出
1.3.1 演示调用
c抛给b,b进行try catch后封装成自己异常,依次类推..
package com.ljf.springboot.mybaits.demos.exdemo;/*** @ClassName: Test* @Description: TODO* @Author: admin* @Date: 2024/11/01 10:33:56 * @Version: V1.0**/
public class Test {public static void main(String[] args) {try {aInfo();} catch (Throwable e) {System.out.println("e:"+e);System.out.println("e1:"+e.getCause());System.out.println("e2:"+e.getCause().getCause());System.out.println("e3:"+e.getCause().getCause().getCause());}}public static void aInfo() {try {bInfo();} catch (ZhengzhouException e) {throw new BeijingException("BException thrown due to AException");// throw new BeijingException("beijing",e);}//https://blog.csdn.net/CSDN_KONGlX/article/details/125486607}public static void bInfo() {// throw new ZhengzhouException("郑州内部异常 throw");try {cInfo();} catch (NullPointerException e) {throw new ZhengzhouException("郑州内部异常 throw");// throw new ZhengzhouException("郑州内部异常 throw",e);}}public static void cInfo(){throw new NullPointerException("内部空指针异常...");}
}
2.执行结果
二 关于throw后还用方法throws的说明
2.1 问题描述
1.有的在方法体内
throw new NullPointerException("内部空指针异常...");
方法上还得throws ....
2.原因在于:
自定义的异常继承于 Exception则要求 严格check; 继承与RuntimeException 则非严格检查。
checked exception就是要强制你去处理这个异常(不管你throws多少层,你终归要在某个地方catch它);而runtime exception则没有这个限制,你可以自由选择是否catch。 https://blog.csdn.net/CSDN_KONGlX/article/details/125486607