栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java的Try/catch吃掉/漏掉Exception的情况

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java的Try/catch吃掉/漏掉Exception的情况

原始代码如下

public String func() throws IOException {
	String res = "_Empty";
	try{
		//IOFunction()
		// the function will throw IOException 
		res = "Success";
	} catch ( ParseException e) {
		e.printStackTrace();
	} finally {
		return res;
	}
}

我在调试这段代码的时候,函数返回结果总是_Empty,但是又没有打印错误信息或者throw IOException 。无意间发现 func() throws 后面的 IOException 是灰色的,同时IDEA提示,该函数永远不会抛出 IOException 。把 finally 语句去掉才正常:

public String func() throws IOException {
	String res = "_Empty";
	try{
		//IOFunction()
		// the function will throw IOException 
		res = "Success";
	} catch ( ParseException e) {
		e.printStackTrace();
	
	return res;
	
}

个人理解,finally 算是对所有 Exception 类型都已经做出了处理,所以不再向上 throws。

欢迎各路大佬批评指正!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/956330.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号