深入理解try catch吃掉异常,及catch(Exception e)中的异常

2026-02-19 07:45:14

代码语言:javascript复制package test.s;

public class yichang {

public static void main(String[] args) throws Exception{

try{

double a=aa();

System.out.println(a);

}catch(Exception e){

e.printStackTrace();

}

}

public static double aa() throws Exception{

double b = 0;

try{

b=1/0;

}catch(Exception e){

throw new Exception(e.getMessage());

}

return b;

}

}输出:

代码语言:javascript复制java.lang.Exception: / by zero

at test.s.yichang.aa(yichang.java:18)

at test.s.yichang.main(yichang.java:6)说明:这算是比较正常的异常写法。aa()方法抛出异常,mian方法捕获异常,并打印出异常原因。

2,

代码语言:javascript复制package test.s;

public class yichang {

public static void main(String[] args) throws Exception{

try{

double a=aa();

System.out.println(a);

}catch(Exception e){

}

}

public static double aa() throws Exception{

double b = 0;

try{

b=1/0;

}catch(Exception e){

throw new Exception(e.getMessage());

}

return b;

}

}没有输出;

说明:这个跟1的区别是main方法捕获aa传来的异常后没有将异常打印出来,所以没有任何输出。

3,

代码语言:javascript复制package test.s;

public class yichang {

public static void main(String[] args) throws Exception{

try{

double a=aa();

System.out.println(a);

}catch(NullPointerException e){

e.printStackTrace();

}

}

public static double aa() throws Exception{

double b = 0;

try{

b=1/0;

}catch(Exception e){

throw new Exception(e.getMessage());

}

return b;

}

}输出:

代码语言:javascript复制Exception in thread "main" java.lang.Exception: / by zero

at test.s.yichang.aa(yichang.java:18)

at test.s.yichang.main(yichang.java:6)说明:在主方法中的catch(nullPointerException e)是空指针异常。而aa()方法抛出来的异常是

ArithmeticException,所以main方法虽然用try catch把aa()方法包裹起来,但是并没有捕获改异常。控制台打印的是java自己处理打印出来的异常。

效果跟下面的代码是一样的:也就是main方法中不用try catch

代码语言:javascript复制package test.s;

public class yichang {

public static void main(String[] args) throws Exception{

double a=aa();

System.out.println(a);

}

public static double aa() throws Exception{

double b = 0;

try{

b=1/0;

}catch(Exception e){

throw new Exception(e.getMessage());

}

return b;

}

}4,

代码语言:javascript复制package test.s;

public class yichang {

public static void main(String[] args) throws Exception{

try{

double a=aa();

System.out.println(a);

}catch(NullPointerException e){

e.printStackTrace();

}

}

public static double aa() throws Exception{

double b = 0;

try{

b=1/0;

}catch(NullPointerException e){

throw new NullPointerException(e.getMessage());

}

return b;

}

}输出:

代码语言:javascript复制Exception in thread "main" java.lang.ArithmeticException: / by zero

at test.s.yichang.aa(yichang.java:16)

at test.s.yichang.main(yichang.java:6)说明这种是catch(NullPointerException e),在aa方法中只能捕获空指针异常,但是b=1/0报的是算术异常,因此也是无法捕获的。使用debug跑程序会发现程序运到b=1/0就打印异常结束程序了。

因此同以下代码:

代码语言:javascript复制package test.s;

public class yichang {

public static void main(String[] args){

double a=aa();

System.out.println(a);

}

public static double aa() {

double b = 0;

b=1/0;

return b;

}

}5,

代码语言:javascript复制 package test.s;

public class yichang {

public static void main(String[] args) throws Exception{

try{

double a=aa();

System.out.println(a);

}catch(NullPointerException e){

e.printStackTrace();

}

}

public static double aa() throws Exception{

double b = 0;

try{

b=1/0;

}catch(ArithmeticException e){

throw new ArithmeticException(e.getMessage());

}

return b;

}

}输出:

代码语言:javascript复制Exception in thread "main" java.lang.ArithmeticException: / by zero

at test.s.yichang.aa(yichang.java:18)

at test.s.yichang.main(yichang.java:6)说明:这中情况也很明显了。aa方法中的try catch 能捕获异常,但是mian方法中的try catch不行

6,最准确的情况

代码语言:javascript复制 package test.s;

public class yichang {

public static void main(String[] args) throws Exception{

try{

double a=aa();

System.out.println(a);

}catch(ArithmeticException e){

e.printStackTrace();

}

}

public static double aa() throws Exception{

double b = 0;

try{

b=1/0;

}catch(ArithmeticException e){

throw new ArithmeticException(e.getMessage());

}

return b;

}

}输出:

代码语言:javascript复制java.lang.ArithmeticException: / by zero

at test.s.yichang.aa(yichang.java:18)

at test.s.yichang.main(yichang.java:6)说明:因为知道aa方法抛出的异常是ArithmeticException,所以准确定位第一层异常捕获。然后在main方法中也精确捕获到aa方法抛来的算术异常。

总结,正确使用try catch 异常,try 不是能吃掉所有的异常,必须要在catch中使用正确的异常才能捕获。但是在实际开发中,很难精确的捕获可能存在的异常。因此我们大多使用第一种情况,exception是所有异常的父类,能捕获到所有的异常。

新增:对于方法套嵌层级很多的,如果在最外层的方法被try catch,那么无论多少层级,最后都会被最外层的try catch捕获到,比如说在实际工作中我们经常会看到这样的代码,最外层的方法被try catch,如果有个方法出现空指针异常,那么最后打印的信息会是最外层catch输出的错误说明。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/106250.html原文链接:https://javaforall.cn