简体   繁体   English

Java与内置异常中抛出自定义异常

[英]Throwing custom exceptions in Java versus built in exceptions

I'm a little confused because I want to be able to throw a custom exception in Java. 我有些困惑,因为我希望能够在Java中引发自定义异常。 To do this inside a function, I have to add throws MyException to the header of the function. 为此,我必须在函数的头中添加throws MyException

And then anything that calls that function in turn must add throws MyException or have a try-catch block. 然后,任何依次调用该函数的函数都必须添加throws MyException或具有try-catch块。 But why is this? 但为什么会这样呢?

For example, when creating a Stack in java and calling the pop function, I don't have to have a try-catch and yet the pop method in Java throws a NoSuchElementException (or w/e it is) if there isn't an element on the stack. 例如,当在Java中创建一个Stack并调用pop函数时,我不必具有try-catch,但是如果不存在Java语言,则Java中的pop方法会抛出NoSuchElementException (或者是)。堆栈上的元素。

NoSuchElementException is a RuntimeException (un-checked exception) and we don't need to handle or declare RuntimeException S, thus the compiler wont complain, but instead throw it at runtime. NoSuchElementExceptionRuntimeException (未经检查的异常),我们不需要处理或声明RuntimeException S,因此编译器不会抱怨,而是在运行时抛出它。

In the case of checked exceptions ie, all the exceptions which are not subtypes of RuntimeException , the compiler will check for a suitable catch clause, enabling the program to continue its course of execution, after performing the operations within the catch ; 在检查的异常,即,所有这一切都没有亚型的例外情况RuntimeException ,编译器会检查是否有合适的catch条款,从而使程序继续执行过程中,执行中的操作后, catch ; to do this, you need to handle them using try/catch blocks or declare them using the throws clause - delegating the responsibility of handling the exception higher up the call chain. 为此,您需要使用try/catch块处理它们或使用throws子句声明它们 - 将处理异常的责任委托给更高级的调用链。 If your custom exception is not a RuntimeException rules of checked exceptions apply to your custom exception as well. 如果您的自定义异常不是RuntimeException检查的异常规则也适用于您的自定义异常。

Checked exceptions ---> Forced by the compiler to handle or propagate 检查异常--->由编译器强制处理或传播

Un-checked exceptions ---> Not checked by the compiler thus appears at runtime 未检查的异常--->编译器未检查因此在运行时出现

Java has so called checked exceptions and unchecked exceptions. Java有所谓的检查异常和非检查异常。 A checked exception must be declared in method signature, an unchecked exception does not need to be declared. 必须在方法签名中声明已检查的异常,而无需声明未检查的异常。

If you want to define an unchecked exception yourself derive it from RuntimeException insted of Exception . 如果要定义未检查的异常,则可以从由Exception插入的RuntimeException派生它。

你应该抛出或尝试捕获所有已检查的异常,它不是RunTimeException必需的,而你所讨论的异常是一个RuntimeException

See my answer to this question. 请参阅我对这个问题的回答。

As a general rule of thumb, when deciding whether or not to create a custom exception class, I try and use the built-in exception classes as much as possible, where it makes sense to do so, eg IllegalArgumentException , etc. 作为一般的经验法则,在决定是否创建自定义异常类时,我尝试尽可能多地使用内置异常类,这样做有意义,例如IllegalArgumentException等。

Here is an example of how to create a custom RuntimeException , which, as you have discovered, will not require a try/catch/finally block, or a throws clause. 这是一个如何创建自定义RuntimeException的示例,正​​如您所发现的那样,它将不需要try/catch/finally块或throws子句。

public class MyRuntimeException extends RuntimeException {
    public MyRuntimeException() {
        super();
    }

    public MyRuntimeException(String message) {
        super(message);
    }
}

public class MyClass {
    public void go(int val) {
        if(val <= 0) throw new MyRuntimeException("Val must be greater than 0.");
    }
}

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

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