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

发布于 2024-09-07 21:15:08 字数 173 浏览 1 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苏别ゝ 2024-09-14 21:15:08

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

  • 缺少括号
  • 缺少分号
  • 对其他类中私有字段的访问
  • 缺少类路径上的类(在编译时)

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

  • 使用实际上为 null 的变量(可能导致 NullPointerException)
  • 在数组上使用非法索引
  • 访问当前不可用的资源(缺少文件,...)
  • 在类路径上缺少类(在运行时)

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

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

  • 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 crashes at 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)

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

只是我以为 2024-09-14 21:15:08

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

Java 语言中有很多很多极端情况。这就是为什么像 Java Puzzlers 这样的东西如此有趣:人们总是无法判断某些东西是否会编译和/或如果确实如此,到底发生了什么?

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

  • 泛型(Eclipse 和 javac 编译器甚至无法在所有方面达成一致)
  • 方法重载解析(JLS 最难理解的部分之一)

相关问题

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.

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

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

  • Generics (Eclipse and javac compiler can't even agree on everything)
  • Method overloading resolution (one of the hardest to understand section of JLS)

Related questions

污味仙女 2024-09-14 21:15:08

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

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文