Java - 何时是编译器错误,什么时候是运行时异常? [英] Java - When is it a compiler error and when is it a runtime exception?

查看:481
本文介绍了Java - 何时是编译器错误,什么时候是运行时异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用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?

推荐答案

编译时错误 - Java编译器无法编译代码,错误。典型的候选人:

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.


  • 使用实际为 null 的变量(可能会导致NullPointerException)

  • 使用数组上的非法索引

  • 访问目前无法使用的资源(缺少文件...)

  • (在运行时)

  • using variable that are actually null (may cause NullPointerException)
  • using illegal indexes on arrays
  • accessing ressources that are currently unavailable (missing files, ...)
  • missing classes on the classpath (at runtime)

('Crashes'术语,仅用于说明发生了什么)

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

这篇关于Java - 何时是编译器错误,什么时候是运行时异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆