Throw exception inside catch clause

前端 未结 7 998
悲哀的现实
悲哀的现实 2021-01-12 15:34

I have two snippet of code :

class PreciseRethrow {
public static void main(String[] str) {
    try {
        foo();
    } catch (NumberFormatException ife)          


        
7条回答
  •  长情又很酷
    2021-01-12 16:10

    Here ,You are throwing new Exception() from catch block but you have mentioned your method foo() can throw only NumberFormatException() which is lower in hierarchy

       static private void foo() throws NumberFormatException {
        try {
        int i = Integer.parseInt("ten");
       } catch (Exception e) {
        throw new Exception();
       }
      }
    

提交回复
热议问题