编译时间与运行时错误 [英] Compile time vs run time errors

查看:202
本文介绍了编译时间与运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

运行时间与编译时间

我应该如何知道某一特定行Java中的代码可能会抛出编译时或运行时错误?假设特定的代码行无论如何都会引发错误。

How should I know whether a specific line of code in Java may throw a compile time or run-time error? Assuming that the specific line of code anyway throws and error.

推荐答案

在Eclipse中,编译时错误将以红色下划线标出。编译时错误是编译器检测到的错误。编译时错误的常见原因包括:

In Eclipse, compile time errors will be underlined in red. A compile time error is an error that is detected by the compiler. Common causes for compile time errors include:


  • 语法错误,例如缺少分号或使用保留关键字(例如'class') )。

  • 当您尝试访问不在范围内的变量时。

  • 当您声明多个具有相同名称的对象时。

如果编译器在编译过程中检测到任何错误,则无法构建新的程序集(或Java中的类文件)。

If the compiler detects any errors during compilation it will fail to build a new assembly (or class file in Java).

即使您的代码没有编译时错误,运行时仍可能发生错误。诸如逻辑错误和运行时错误之类的错误。运行时错误的一个很好的例子如下:

Even if your code has no compile time errors, errors can still occur on run-time. Errors such as 'logic errors' and 'runtime errors'. A good example of a runtime error is as followed:


  • 假装你要在索引5的数组中存储一个项目,但是数组的大小只有4.编译器不会在这里检测到错误,因为它知道数组大小可能会发生变化,但是在运行时你会被抛出异常。

要检测确切地发生运行时错误的行,可以在Eclipse中使用断点组合并进行适当的异常处理。

To detect which line exactly a run-time error occurs on you can use a combination of break points in Eclipse and proper exception handling.

这篇关于编译时间与运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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