简体   繁体   English

我应该抛出什么异常

[英]What exception should I throw

I am a newbie and learning Java exceptions, 我是新手,正在学习Java异常,

public void function (argument) {
     if (condition) {
        throw new Exception;
     }
}

My confusion is: 我的困惑是:

If I know like the condition will cause NullPointerException, so I can throw a NullPointerException. 如果我知道这种情况会导致NullPointerException,则可以抛出NullPointerException。

What if the code throw some Exceptions I didn't expect, or say I don't know what is the Exceptions of my code, what should I throw? 如果代码抛出了一些我没想到的异常,或者说我不知道​​代码的异常是什么,该怎么办?

Like this link When to throw an exception? 像这个链接什么时候抛出异常? said: "Every function asks a question. If the input it is given makes that question a fallacy, then throw an exception." 说:“每个函数都会问一个问题。如果给出的输入使该问题成为谬论,则抛出异常。”

but if the input does make a question a fallacy, but myself don't know this input will cause this fallacy, what should I throw? 但是,如果输入确实使一个问题成为谬论,但我自己不知道该输入会导致这种谬论,那么我该怎么办?

Or I should run enough test to find all exceptions and throw them? 还是我应该进行足够的测试以找到所有异常并将其抛出?

I know my question is weird, but if you know what am I saying, please give me some instructions. 我知道我的问题很奇怪,但是如果您知道我在说什么,请给我一些指示。 Thanks 谢谢

Source: Oracle - The Java Tutorials - " What Is an Exception? ": 来源:Oracle-Java教程-“ 什么是例外? ”:

"After a method throws an exception, the runtime system attempts to find something to handle it. The set of possible "somethings" to handle the exception is the ordered list of methods that had been called to get to the method where the error occurred.". “在方法引发异常之后,运行时系统会尝试查找要处理的东西。处理异常的可能“东西”的集合是被调用以获取发生错误的方法的方法的有序列表。 ”。

Each function that doesn't directly provide a means to handle an exception returns to a caller with either a successful result or an unhandled exception for the caller to handle. 每个不直接提供处理异常的方法的函数都会以成功的结果或未处理的异常供调用方返回,以供调用方处理。

A function that might encounter an exception and is able to handle it avoids the need to have the caller handle it, similarly a caller that can handle exceptions from its subroutines saves writing the handler in the subroutines. 一个可能遇到异常并能够处理它的函数避免了让调用者处理它的需要,类似地,可以处理其子例程中的异常的调用者将处理程序写入子例程中。

If the caller is calling various subroutines that might all encounter the same error conditions then handling it in the caller results in less code (and consistency in the handling of the exception) over rewriting similar code in each subroutine which would be better handled by the caller. 如果调用者正在调用可能都遇到相同错误条件的各种子例程,则在每个子例程中重写类似代码比在每个子例程中重写类似代码会更好地减少代码(以及异常处理的一致性),这将更好地由调用者处理。

Source: Oracle - The Java Tutorials - " Unchecked Exceptions — The Controversy ": 来源:Oracle-Java教程-“ 未经检查的异常-争议 ”:

"If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.". “如果可以合理地期望客户端从异常中恢复,则将其设置为已检查的异常。如果客户端无法采取任何措施从异常中恢复,则将其设置为未检查的异常。”

Try to predict what could happen and handle it if possible, always trying to let the caller do the work if it's duplicated in multiple callees and having the 'leaves of the tree catch the light work'. 试图预测发生什么,如果可能,处理它,总是试图让呼叫者做的工作,如果它在多个被调用者拥有副本,并具有“树的叶子捕捉光线的工作”。

Or I should run enough test to find all exceptions and throw them? 还是我应该进行足够的测试以找到所有异常并将其抛出?

Writing a test harness can be separated or part of the code, if it's internal then usually (but not always) you'd want to define it out of the release version. 编写测试工具可以是单独的,也可以是代码的一部分,如果它是内部的,则通常(但不总是)您希望在发行版本之外进行定义。

I think an exception should be thrown: 我认为应该抛出异常:

  • if a function cannot satisfy an established condition 如果功能不能满足既定条件

  • it cannot satisfy the precondition of a function it is about to call 它不能满足将要调用的函数的先决条件

  • if this can cause instability for other members 如果这可能导致其他成员不稳定

There are some other situations that can be taken into consideration, but basically for me these are the main things to keep in mind. 还可以考虑其他一些情况,但是基本上对我而言,这是需要牢记的主要内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM