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

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

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;
	}

}

输出:

java.lang.Exception: / by zero
	at test.s.yichang.aa(yichang.java:18)
	at test.s.yichang.main(yichang.java:6)

说明:这算是比较正常的异常写法。aa()方法抛出异常,mian方法捕获异常,并打印出异常原因。

2,

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,

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;
	}

}

输出:

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

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,

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;
	}

}

输出:

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就打印异常结束程序了。

因此同以下代码:

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,

	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;
		}
	
	}

输出:

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,最准确的情况

	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;
		}
	
	}

输出:

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.net/106250.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • AMD CPU安装Intel HAXM

    AMD CPU安装Intel HAXM1.2.找到安装目录(我的安装目录:F:\Android\SDK\extras\google\Android_Emulator_Hypervisor_Driver)下的这个文件:silent_install,右击该文件选择“以管理员运行”,即可。…

    2022年6月28日
    83
  • windows环境下tomcat部署war包[通俗易懂]

    windows环境下tomcat部署war包[通俗易懂]1.从Eclipse中将项目打包成war包,Runas>>MavenInstall,然后将war包放在tomcat\webapps下,如图所示2.然后运行tomcat\bin目录下的startup.bat(运行后悔在webapps下生成相应名称的目录)3.运行成功,使用Postman测试成功…

    2022年6月2日
    48
  • LaTeX数学公式-详细教程

    LaTeX数学公式-详细教程LaTeX数学公式,包含前言,注意事项,插入公式,注释,编号,转义字符,换行与对齐,字体,空格,上下标,括号,大括号和行标,分式,开方,对数,省略号,最值,方程组和分段函数,累加和累乘,矢量,积分,极限,导数与偏导,矩阵,表格,希腊字母,运算符,戴帽符号,特殊符号,等等。

    2022年5月23日
    34
  • 微信模拟地理位置_微信伪装地理位置是什么个原理「建议收藏」

    微信模拟地理位置_微信伪装地理位置是什么个原理「建议收藏」展开全部微信的定位数据来源主要有,基站定位、GPS定位。原理主体为:通过劫持代码关闭636f70793231313335323631343130323136353331333366306537从基站获取位置程序运行,伪造CELLID伪造WIFIMAC地址通过Xposed模块进行模拟地理位置、基站信息并上传伪装信息到微信客户端进行系统欺骗进而达到伪装地理位置的目的。扩展资料:微信定位功能实践上…

    2022年6月13日
    34
  • Redis

    Redis

    2021年3月12日
    160
  • 保研面试/考研复试:英语口语面试必备话题及常用句型句式整理(一)[通俗易懂]

    目录StudiesAFFIRMATIVENEGATIVEWorkForAgainstHometownAFFIRMATIVENEGATIVESportFORAGAINSTStudiesAFFIRMATIVEMyuniversityhasagreatreputation.Thisisaprettypromisingfield.Itenablesmetofulfillmypotential.Myprofessorsarereallythoughtfuland

    2022年4月9日
    79

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号