出现异常后,catch块中的代码为什么不运行?

淡的微笑 2015-05-12 11:46:23
下面这个方法用于打开一个properties文件,抛出FileNotFoundException异常。
static void init() throws FileNotFoundException{
String configFilename = null;
URL url = ClassLoader.getSystemResource("");
String subStr = url.toString().substring(6);
PropertyConfigurator.configure(subStr+FinalProperty.getLogRelativePath());
}
在主函数中调用此方法并捕捉到异常,但是catch中的代码并没有运行,我原本准备在catch中运行另一种获取文件的代码用以处理前面的异常。
try {
Init.init();
} catch (IOException e) {
// TODO 自动生成的 catch 块
System.out.println("sssssssssssssssss");
}





...全文
662 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
scmod 2015-05-17
  • 打赏
  • 举报
回复
是不是抛出的是空指针啊... 用catch exception看看
艺匠之美 2015-05-17
  • 打赏
  • 举报
回复
捕获的异常不对,IOException异常范围太小
S117 2015-05-12
  • 打赏
  • 举报
回复

try {
	Init.init();
} catch (IOException e) {
// TODO 自动生成的 catch 块
   System.out.println("sssssssssssssssss");
}catch(FileNotFoundException e){
	//FileNotFoundException
}catch(Exception e){
	//所有其他异常
}
风吹过夏天 2015-05-12
  • 打赏
  • 举报
回复
引用 6 楼 a1129963143 的回复:
[quote=引用 5 楼 u012873407 的回复:] 估计拋的不是IO异常,FileNotFoundException
IOException是FileNotFoundException的父异常吧[/quote] 手动new一个throw试试
淡的微笑 2015-05-12
  • 打赏
  • 举报
回复
static{ try { Init.init(); } catch (FileNotFoundException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); System.out.println("出现FileNotFoundException异常。"); } } 还行不行啊,我将要读取的文件名改为错误,JAVA虚拟机自动捕捉到FileNotFoundException异常,我写成 try { Init.init(); } catch (FileNotFoundException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); System.out.println("出现FileNotFoundException异常。"); }catch(IOException io){ System.out.println("IO异常。"); }catch(Exception e){ System.out.println("出现异常。"); } 这样还是不想,手动捕捉不到异常,手写的异常捕捉代码完全没效果啊。
淡的微笑 2015-05-12
  • 打赏
  • 举报
回复
引用 5 楼 u012873407 的回复:
估计拋的不是IO异常,FileNotFoundException
IOException是FileNotFoundException的父异常吧
风吹过夏天 2015-05-12
  • 打赏
  • 举报
回复
估计拋的不是IO异常,FileNotFoundException
  • 打赏
  • 举报
回复
try { Init.init(); } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace();//打印异常信息 System.out.println("sssssssssssssssss"); }
Coder_D 2015-05-12
  • 打赏
  • 举报
回复
捕获的异常不对,你抛出IOException才会进入catch语句块
一大三千 2015-05-12
  • 打赏
  • 举报
回复
因为你catch的是IOException,当然无法执行啦,改成如下

try {
  Init.init();
} catch (FileNotFoundException e) {
   // TODO 自动生成的 catch 块
   System.out.println("sssssssssssssssss");
} catch(Exception e){
    //所有其他异常
}

50,526

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧