简体   繁体   English

每个例外都有必要的试用吗?

[英]Does every exception have an required try-catch?

A simple question and I can't find the answer for it. 一个简单的问题,我找不到答案。 Is it required to every exception in Java to use a try-catch? Java中的每个异常都需要使用try-catch吗? Or is it only for the FileNotFoundException? 或者仅适用于FileNotFoundException?

A lot of exceptions (IndexOutOfBoundException, ArithmeticException, IlligalArgumentException, NullPointerException) are saying that they didn't need an Exception, but FileNotFoundException does)... and I can't find the answer which do and which doesn't need try-catch. 很多异常(IndexOutOfBoundException,ArithmeticException,IlligalArgumentException,NullPointerException)都表示他们不需要Exception,但是FileNotFoundException会这样做...而且我找不到答案,哪些不需要try-catch 。

It's not absolutely required to have a try/catch block for your exceptions. 对于您的异常,并不是绝对需要try/catch块。 Instead, you can throw them to someone who is able to handle the exception properly. 相反,您可以throw它们throw能够正确处理异常的人。

There are 2 kinds of exceptions: Checked and Unchecked. 有两种例外:已选中和未选中。 A Checked exception can be considered one that is found by the compiler, and the compiler knows that it has a chance to occur, so you need to catch or throw it. Checked异常可以被认为是编译器找到的异常,并且编译器知道它有可能发生,所以你需要catchthrow它。 For example, opening a file. 例如,打开文件。 It has a chance to fail, and the compiler knows this, so you're forced to catch or throw the possible IOException . 它有可能失败,编译器知道这一点,所以你不得不catchthrow可能的IOException

An Unchecked exception can be considered one that has a chance of occurring, but based on your code, the compiler doesn't know. 未经检查的异常可被视为有可能发生的异常,但基于您的代码,编译器不知道。 In other words, it's a programming error . 换句话说,这是一个编程错误 For example, if you're taking user input and expect a number, and the user enters something you didn't expect, such as a string, your program would throw a NumberFormatException . 例如,如果您正在接受用户输入并期望一个数字,并且用户输入了您不期望的内容(例如字符串),则您的程序将抛出NumberFormatException You can predict these scenarios and put try/catch to try and avoid them before they occur. 您可以预测这些场景并将try/catch为在它们发生之前尝试避免它们。 Very rarely seen is a person adding a throws NullPointerException or throws NumberFormatException (or throwing any other Unchecked exception, for that matter). 很少见到一个人添加一个throws NullPointerExceptionthrows NumberFormatException (或抛出任何其他未经检查的异常,就此而言)。 It's allowed, but explicitly creating that exception is weird and most people would say that it's bad coding style. 这是允许的,但明确地创建该异常是很奇怪的,并且大多数人会说这是不好的编码风格。

Note that all Checked suggestions must be caught or thrown to something that can handle it; 请注意,所有Checked建议必须被捕获或抛出到可以处理它的东西; if you don't do it, your program won't compile. 如果你不这样做,你的程序将无法编译。 If you throw it to something that can't handle it, then your program will likely crash if it occurs. 如果你把它扔到无法处理的东西上,那么你的程序如果发生就会崩溃。

Also note that an unchecked Exception (eg: one that occurs during runtime, usually via bad user input or whatnot) will also usually crash your program. 另请注意,未经检查的异常(例如:在运行时发生的异常,通常是通过错误的用户输入或诸如此类)也会导致程序崩溃。 Thus, it's usually a good idea to use try/catch when something can potentially go wrong, but you don't have to . 因此,当某些东西可能出错时使用try/catch通常是个好主意,但是你不必这样做

Also interesting to note is that while Checked exceptions are subclasses of Exception and Unchecked exceptions are subclasses of RuntimeException, RuntimeException itself is a subclass of Exception. 另外值得注意的是,Checked异常是Exception的子类,Unchecked异常是RuntimeException的子类,RuntimeException本身是Exception的子类。 That means that if you really wanted to, a single try {} catch (Exception e) {} will catch every single exception your program could possibly throw. 这意味着如果你真的想要,一个try {} catch (Exception e) {}将捕获你的程序可能抛出的每个异常。 Granted, this is considered a horrible way to deal with exceptions, and you should catch each one separately so that you can handle them separately. 当然,这被认为是处理异常的可怕方式,您应该分别捕获每个异常,以便您可以单独处理它们。 Please try not to use it. 请尽量不要使用它。

No, not every exception requires a try-catch. 不,并非每个例外都需要试一试。 Every checked exception requires a try catch. 每个检查过的异常都需要try catch。 For example, a NullPointerException is an unchecked exception, so it does not require a try-catch, whereas a FileNotFoundException is checked, so it does require one. 例如,NullPointerException是一个未经检查的异常,因此它不需要try-catch,而是检查FileNotFoundException,因此它确实需要一个。 You can also add "throws" to the method signature and thus avoid needing a try-catch. 您还可以向方法签名添加“throws”,从而避免需要try-catch。

Read: https://docs.oracle.com/javase/tutorial/essential/exceptions/ 阅读: https//docs.oracle.com/javase/tutorial/essential/exceptions/

Basically checked Exceptions need to be handled or thrown Unchecked Exceptions and Errors may be handled or thrown (although handling Error is in general considered bad practise). 基本上检查的异常需要处理或抛出可以处理或抛出未经检查的异常和错误(尽管处理错误通常被认为是不好的做法)。

Checked exception is everything that inherits from java.lang.Exception 检查异常是从java.lang.Exception继承的所有内容

Unchecked exception is everything that inherits from java.lang.RuntimeException 未经检查的异常是从java.lang.RuntimeException继承的所有内容

Error is everything that inherits from java.lang.Error 错误是从java.lang.Error继承的所有内容

只有Checked异常显式需要捕获它,对于其他所有类型的异常,您可以使用“throws”方法签名。

Yes, but if you don't want to handle it in your method you can pass the exception to the caller of the method with the throws keyword. 是的,但是如果您不想在方法中处理它,可以使用throws关键字将异常传递给方法的调用者。 Example: 例:

void execption() throws Exception {
    throw new Exception();
}

void caller() {
    try {
        execption();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Edit: I'm a bit rusty on my java, like josh said you can have unchecked exceptions that don't need a try/catch like NullPointerException , but you can add one if you think an unchecked exception may be thrown. 编辑:我在我的java上有点生疏,就像josh说你可以拥有不需要像NullPointerException那样的try / catch的未经检查的异常,但如果你认为可能会抛出未经检查的异常,你可以添加一个。 Example: 例:

Object obj = null;

obj.hashCode();// if you think a NPE will be thrown you can use a try/catch here

When a method that you call explicitly throws an exception then you have to use try....catch loop. 当您调用的方法显式抛出异常时,您必须使用try....catch循环。 But in case of the list you have given are all runtime exceptions. 但是,如果您给出的列表都是运行时异常。 They get thrown when sometimes a program has inputs that were not expected or the program was put to some use that it was not intended for. 当有时某个程序有不期望的输入或程序被用于某些不适合的用途时,它们会被抛出。 These would not require a try....catch loop. 这些不需要try....catch循环。

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

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