为什么 Catch(Exception) 几乎总是一个坏主意? [英] Why is the Catch(Exception) almost always a bad Idea?

查看:31
本文介绍了为什么 Catch(Exception) 几乎总是一个坏主意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 catch(Exception) 几乎总是一个坏主意?

Why is the catch(Exception) almost always a bad Idea?

推荐答案

因为当您捕获异常时您应该正确处理它.并且您不能期望在您的代码中处理所有类型的异常.此外,当您捕获所有异常时,您可能会遇到无法处理的异常,并阻止堆栈中的上层代码正确处理它.

Because when you catch exception you're supposed to handle it properly. And you cannot expect to handle all kind of exceptions in your code. Also when you catch all exceptions, you may get an exception that cannot deal with and prevent code that is upper in the stack to handle it properly.

一般原则是尽可能捕捉最具体的类型.

The general principal is to catch the most specific type you can.

这篇关于为什么 Catch(Exception) 几乎总是一个坏主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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