异常处理问题 [英] Exception Handling Question

查看:117
本文介绍了异常处理问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有关异常处理的问题。
考虑以下Java代码片段。

I have a question regarding exception handling. Consider following Java code snippet.

        try{
            //code
        }catch(SubSubException subsubex){
            //code
        }catch(SubException subex){
            //code
        }catch(Exception ex){
            //code
        }

我知道这是处理异常的推荐方法。但是我可以使用以下代码片段来实现相同的事情。

I know this is the recommended way of handling exceptions. But I can achieve the same thing by using following code snippet.

        try{
            //code
        }catch ( Exception ex){
            if( ex instanceof SubException){              
                //code
            }else if(ex instanceof SubSubException){
                //code
            }else{
                //code
            }
        }

有人可以告诉我第二种方法的缺点?

Can somebody tell me disadvantages of second approach?

推荐答案

第二种方法较不可读。此外,宠物小精灵异常处理永远不会有办法,即使您的聪明技巧是使用instanceof关键字。无论如何,我不是在玩耍或嘲笑你,但最好是为人类编写代码来阅读和维护,而不是为了电脑。

The second approach is less readable. In addition Pokemon exception handling is never the way to go even though your "clever" trick is to use the instanceof keyword. I am not poking fun or mocking you in anyway, but it is best to write code for humans to read and maintain, not for the computer.

这篇关于异常处理问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆