java – 在catch块中抛出相同的异常
发布时间:2020-12-15 04:31:00 所属栏目:Java 来源:网络整理
导读:我有以下2个代码片段,我想知道是什么让 java编译器(在 Eclipse中使用Java 7)显示第2个片段的错误,为什么不为第1个片段. 以下是代码段: 摘录1 public class TestTryCatch { public static void main(String[] args) { // TODO Auto-generated method stub Sy
我有以下2个代码片段,我想知道是什么让
java编译器(在
Eclipse中使用Java 7)显示第2个片段的错误,为什么不为第1个片段.
以下是代码段: 摘录1 public class TestTryCatch { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(get()); } public static int get(){ try{ System.out.println("In try..."); throw new Exception("SampleException"); }catch(Exception e){ System.out.println("In catch..."); throw new Exception("NewException"); } finally{ System.out.println("In finally..."); return 2; } } } 摘录2 public class TestTryCatch { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(get()); } public static int get(){ try{ System.out.println("In try..."); throw new Exception("SampleException"); }catch(Exception e){ System.out.println("In catch..."); throw new Exception("NewException"); } // finally{ // System.out.println("In finally..."); // return 2; // } } } 在eclipse中,snippet1显示为finally块添加’SuppressWarning’,但在snippet2中,它显示为catch块中的throw语句添加’throws或try-catch’块. 我详细研究了以下问题,但他们没有提供任何具体的理由. > exception-thrown-inside-catch-block-will-it-be-caught-again 解决方法
finally块应始终执行.这是主要原因.在catch块中抛出异常,但在finally块掩码异常中执行return语句.删除finally块或将finally语句移出finally块.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |