尝试捕获中的Exception与JSONException之间的区别

问题描述 投票:1回答:1

我对在Exception中使用不同的Try-Catch术语感到困惑。在这里,使用简单的disadvantage而不是Exception可能是什么JSONException

都在这里:

简单异常

       try {
          //...
       }catch (Exception e){
                e.printStackTrace();
       }

JSONException

       try {
          //...
       }catch (JSONException e){
                e.printStackTrace();
       }
android exception
1个回答
0
投票

[Exception是所有例外classIOExceptionIndexOutOfBoundsExceptionFileNotFoundExceptionNullPointerException等的基础JSONException

如果您使用try/catch,如下所示:

 try {
          //...
       }catch (Exception e){
                e.printStackTrace();
       }

上面提到的所有exceptions都将被捕获,并且在任何这些例外情况下,您的catch块都将获得executed/called

但是如果您使用如下所示的try/catch

try {
          //...
       }catch (JSONException e){
                e.printStackTrace();
       }

仅当有catch时,您的called/executed块才会获得JSONException

如果ex NullPointerExceptionIndexOutOfBoundsException等存在其他类型的异常,则不会调用您的catch块。

希望这消除了您的疑问/问题。

© www.soinside.com 2019 - 2024. All rights reserved.