简体   繁体   English

已检查和未检查的异常如何在 Java 中工作?

[英]How do Checked and Unchecked Exceptions work in Java?

Reading about Exceptions in this book , I found this statement:阅读本书中的异常时,我发现了以下语句:

Checked exceptions are checked by the compiler at compile time.编译器在编译时检查已检查的异常。

and

The compiler does not check unchecked exceptions at compile time.编译器不会在编译时检查未经检查的异常。

So, if also we can say that IOException or SQLException are below the Checked Exceptions class tree.因此,如果我们也可以说IOExceptionSQLException在 Checked Exceptions class 树的下方。 How would java compiler know there will be an exception and no for IllegalArgumentException which might remain inside the code as my understanding. java 编译器如何知道会有一个异常,而IllegalArgumentException不会,据我所知,它可能会保留在代码中。

Also, what does exactly the fact of being obligatory to get the Checked exceptions caught and not for Unchecked mean?此外,必须捕获已检查异常而不是未检查异常这一事实到底意味着什么?

A checked exception requires that you add a throws declaration to the method signature, or you add a try catch block around it. 受检查的异常要求您将throws声明添加到方法签名,或在其周围添加try catch块。

public void checked() throws IOException {
  throw new IOException(); // compiles just fine
}

public void checked() {
  try {
    throw new IOException();
  } catch (IOException e) {
    // ...
  }
}

This will not work: 这将不起作用:

public void checkedWithoutThrows() {
  throw new IOException(); // will not compile
}

An unchecked exception does not need this. 未检查的异常不需要此。

public void unchecked() {
  throw new RuntimeException();
}

And, just for completeness, the way to determine unchecked from checked exceptions is that unchecked exceptions all descend from RuntimeException at some point or another. 而且,仅出于完整性考虑,从受检查的异常中确定未受检查的异常的方法是,所有未受检查的异常都在某个时刻或其他时刻源自RuntimeException

exceptions in java all work the same way. Java中的异常均以相同的方式工作。
the difference between checked and unchecked exceptions (which are all subclasses of RuntimeException ) if that for a method that throws a checked exception whoever calls that method has to either try/catch the exception, or declare his own method as throwing that exception. 如果抛出异常的方法抛出了异常,那么调用该方法的人必须尝试/捕获异常,或者声明自己的方法抛出该异常,这就是已检查异常和未检查异常(都是RuntimeException的子类)之间的区别。

so if i have a method: 所以,如果我有一种方法:

void throwsACheckedException() throws SomeCheckedException;

whoever calls it must do one of 2 things. 叫它的人必须做2件事之一。 either: 要么:

try {
   throwsACheckedException();
} catch (SomeCheckedException e) {
   //do something
}

or 要么

void someCallingMethod() throws SomeCheckedException { //pass it on
   throwsACheckedException();
}

unchecked exceptions you dont have to declare, and whoever calls the method does not have to explicitly catch. 无需声明的未经检查的异常,调用该方法的任何人都不必显式捕获。 for example: 例如:

void someInnocentLookingMethod() {
   throw new NullPointerException("surprise!"); //...extends RuntimeException
}

and then you can simply invoke it, without the try/catch hassle: 然后您可以简单地调用它,而不会遇到try / catch的麻烦:

void unsuspectingVictim() {
   someInnocentLookingMethod();
}

unchecked exceptions are usually used for things that can creep up on you at any point and so forcing developers to try/catch them would make the code very tedious (NullPointerException, for example), although there are those who thing checked exceptions are evil entirely :-) 未经检查的异常通常用于随时可能蔓延到您的事物,因此强迫开发人员尝试/捕获它们会使代码非常乏味(例如,NullPointerException),尽管有些人将事物检查的异常完全视为罪恶: -)

For Checked exceptions, you see error at compile time and says that you have to handle them. 对于Checked异常,您会在编译时看到错误,并说必须处理它们。 Run time exceptions do not give any error or warning to handle in your code. 运行时异常不会给您的代码带来任何错误或警告。

Compiler expects exceptions (Checked exceptions) and asks you to handle it. 编译器需要异常(已检查的异常)并要求您处理。

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

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