Java - 什么时候是编译器错误,什么时候是运行时异常?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3179504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:09:14  来源:igfitidea点击:

Java - When is it a compiler error and when is it a runtime exception?

javaexceptioncompilationscjp

提问by Michael

I am currently studying for the SCJP certification using the Sierra and Bates Study Guide and in many of the self tests (mock exam questions) I keep running into the same problem - I can't tell whether a particular error will be at runtime (an exception) or at compile (compile error). I know this is a bit of a vague question and that it might not be possible to answer but, how can I tell if an error will be found at compile or at runtime? Would you be able to send me some website links that might be able to help me?

我目前正在使用 Sierra 和 Bates 学习指南学习 SCJP 认证,并且在许多自测(模拟考试题)中,我一直遇到同样的问题 - 我不知道是否会在运行时出现特定错误(异常)或在编译(编译错误)。我知道这是一个有点模糊的问题,可能无法回答,但是,我如何判断在编译时还是在运行时会发现错误?您能否给我发送一些可能对我有帮助的网站链接?

采纳答案by Andreas Dolk

Compile time error- the java compiler can't compile the code, often because of syntax errors. Typical candidates:

编译时错误- java 编译器无法编译代码,通常是因为语法错误。典型候选人:

  • missing brackets
  • missing semicolons
  • access to private fields in other classes
  • missing classes on the classpath (at compile time)
  • 缺少括号
  • 缺少分号
  • 访问其他类中的私有字段
  • 类路径上缺少类(在编译时)

Runtime error- the code did compile, can be executed but crashesat some point, like you have a division by zero.

运行时错误- 代码确实编译了,可以执行但在某些时候崩溃,就像你被零除一样。

  • using variable that are actually null(may cause NullPointerException)
  • using illegal indexes on arrays
  • accessing resources that are currently unavailable (missing files, ...)
  • missing classes on the classpath (at runtime)
  • 使用实际的变量null(可能导致 NullPointerException)
  • 在数组上使用非法索引
  • 访问当前不可用的资源(丢失的文件,...)
  • 类路径上缺少类(在运行时)


('Crashes' is really not the correct term and is only used to illustrate what happens)

(“崩溃”确实不是正确的术语,仅用于说明发生的情况)

回答by polygenelubricants

There is no easy answer to this; to see if something will compile, you have to completely understand the language specification and the API involved. You essentially have to act like a compiler, and no one can do this perfectly. Even compilers don't always follow the specification perfectly.

对此没有简单的答案。要查看是否可以编译某些内容,您必须完全了解所涉及的语言规范和 API。您本质上必须像编译器一样工作,没有人可以完美地做到这一点。甚至编译器也不总是完全遵循规范。

There are many, MANYcorner cases in the Java language. This is why things like Java Puzzlersare so intriguing: people can't always tell if something would even compile and/or if it does, what's really going on.

Java 语言中有很多很多极端情况。这就是Java Puzzlers 之类的东西如此有趣的原因:人们无法总是判断某些东西是否可以编译和/或如果可以编译,到底发生了什么。

Some of the more complicated areas of the Java language are:

Java 语言的一些更复杂的领域是:

  • Generics (Eclipse and javaccompiler can't even agree on everything)
  • Method overloading resolution (one of the hardest to understand section of JLS)
  • 泛型(Eclipse 和javac编译器甚至不能在所有事情上达成一致)
  • 方法重载解析(JLS 中最难理解的部分之一)

Related questions

相关问题

回答by JediCate

Basically Runtime errors are logical errors in your code, even if the code is syntactically correct. Compiler errors refer to errors in your syntax/semantics. If you have a compiler error in your code, the program will never get to run (and check the logic of the code). If you have both syntactic and logical errors, you will first get the compiler error (syntax error) and then when you run the code again you will get a runtime error (logical error).

基本上运行时错误是代码中的逻辑错误,即使代码在语法上是正确的。编译器错误是指语法/语义中的错误。如果您的代码中有编译器错误,程序将永远无法运行(并检查代码的逻辑)。如果你同时有语法和逻辑错误,你首先会得到编译器错误(syntax error),然后当你再次运行代码时你会得到一个运行时错误(逻辑错误)。