如果捕捉空指针异常不是一个好的做法,捕捉异常是一个好的例子吗? [英] If catching null pointer exception is not a good practice, is catching exception a good one?

查看:248
本文介绍了如果捕捉空指针异常不是一个好的做法,捕捉异常是一个好的例子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说过捕捉 NullPointerException 是一个不好的做法,我认为这是明智的。让 NullPointerException 传播到顶部将允许检测出错的东西。但是很多时候,我看到很多朋友直接捕捉到 Exception ,这样他们就不用担心上述代码中可能发生的所有不同种类的异常。这是一个好习惯吗?什么是其他种类的例外,最好留下未处理的?此外,除了我们确定异常来源的特定代码之外,我还需要处理 NullPointerException 。那么什么时候处理的例外是什么时候什么时候不被处理?而最好的未处理的例外列表可能是什么?

I have heard that catching NullPointerException is a bad practice, and i think it is sensibly so. Letting the NullPointerException to propagate to the top would allow the detection of a something going wrong. But many times I have seen many of my friends catching Exception directly so that they need not bother about all the different kinds of exceptions that might occur in the above code. Is this a good practice? What are the other kinds of exceptions that are best left unhandled? And besides it also makes sense to me to handle NullPointerException over a specific code where we are sure of the source of the exception. So when are exceptions to be handled and when should they not be handled? And what would be the possible list of exception that are best left unhandled?

推荐答案

Pokemon异常处理是坏的。特别是,如果它是一个空的块,你只是吞咽它们。您有特定类型的异常,因为它们实际上是指具体上下文中的具体内容(实质上是告诉您出现了什么问题)。所以通过捕捉异常你说你不在乎这些例外是什么,你不在乎发生了什么。这可能不是你想要的。

Pokemon exception-handling is bad. Especially, if it's an empty block and you're simply swallowing them. You have specifically-typed exceptions for the reason that they actually mean specific things in specific contexts (essentially they're telling you what went wrong). So by catching Exception you're saying that you don't care what those exceptions are and that you don't care what happened. This is probably not what you want.

一般来说,捕捉异常时,遵循以下规则:

In general, when catching exceptions follow these rules:


  • 在这个级别处理异常是否有意义?如果是,那么处理它。如果没有,则传播。

  • 结合第一条规则,处理也可以意味着捕获,包装和重新抛出。这是一种防止抽象泄漏的方法,使您的方法的调用者不必了解底层实现。

  • 一个空的catch块并不意味着你已经处理例外。这就是所谓的吞咽至少要记录异常。有时发生异常实际上是你的代码的逻辑流程的一部分,所以你可能想做一些特别的事情(但这是赦免双关语,异常而不是规则。最好检查导致异常的情况而不是将它们并入您的代码的逻辑流程中)

您可以轻松地检查代码中的空值,因此不需要明确地捕获一个空指针异常。让$ code> NullPointerException 发生(这是不好的做法)是没有意义的。即使你有一些代码抛出一个 NullPointerException ,它是你不能控制,无法修复的代码,你应该确定导致 NullPointerException 并专门测试他们。

You can easily check for a null value in your code, so there is no need to explicitly catch a null-pointer exception. It doesn't make sense to let a NullPointerException happen (and it's bad practice). Even if you have some code that throws a NullPointerException, and it is code that you do not control and cannot fix, you should determine the input parameters that cause the NullPointerException and specifically test for them.

您不应该捕获的另一个异常是 IllegalArgumentException 。这个异常意味着你已经通过了一个没有意义的参数。您应该明确测试输入参数,以确保它们是理智的,并且不会导致 IllegalArgumentException

Another exception that you shouldn't catch is the IllegalArgumentException. This exception means that you've passed in an argument that does not make sense. Instead of catching this exception, you should explicitly test your input parameters to ensure that they are sane and that they cannot cause an IllegalArgumentException.

这篇关于如果捕捉空指针异常不是一个好的做法,捕捉异常是一个好的例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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