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

查看:29
本文介绍了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:

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

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

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

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

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