简体   繁体   English

Java术语:为什么要在编译时出错而不在编译时例外?

[英]Java Terminology: Why compile-time error and not compile-time exception?

This may sound awkward ... 这听起来很尴尬...
But I didn't understand it. 但是我不明白。

Why do we have compile-time error and not compile-time exception in java ? 为什么我们在Java中有compile-time error而不是compile-time exception

I mean to say that we never say compile-time exception. 我的意思是说我们永远不会说compile-time exception.
We tend to say it as compile-time error . 我们倾向于将其称为compile-time error

Is there any specific reason for the same ?? 是否有相同的任何具体原因?
Any suggestions are welcomed.... 欢迎任何建议。

Thanks ! 谢谢 !

The reason for this is that an exception is something thrown during execution of a program. 这样做的原因是在程序执行期间抛出了异常。 Java has a specific type for this, the Exception class. Java为此具有特定类型,即Exception类。

At compile time, your code is not executing, so it cannot throw an exception. 在编译时,您的代码未执行,因此它不会引发异常。 Indeed, it is proper execution of the compiler to find errors in your code - certainly not an exception case! 确实,正确执行编译器以查找代码中的错误-当然不是例外情况!

Exception in java is really different than compile error. Java中的异常与编译错误确实不同。 We don't have the term compile time exception. 我们没有术语“编译时异常”。 Because exception is something happens that you don't expect it to happen. 因为例外是发生的事情,所以您不希望发生。 We only have checked and unchecked exception. 我们只检查和未检查异常。 With checked exception, in compile time , the compiler will force you to catch it, but it is not an error . 如果有检查异常,则在编译时 ,编译器将强制您捕获它,但这不是错误 Don't catch it, you can not compile the program but it is not a compile error. 不要抓住它,您无法编译程序,但这不是编译错误。

An error indicates that there is a problem with the program. 错误表明程序有问题。 An exception is a specific construct that interrupts the control flow of the program, and unwinds the stack, capturing information about the state of the stack so that it can be reported. 异常是一种特殊的构造,该构造会中断程序的控制流,并展开堆栈,捕获有关堆栈状态的信息,以便可以对其进行报告。

An exception can be used to indicate an error, but not always. 异常可用于指示错误,但并非总是如此。 For example: 例如:

void startOperation() {
 try {
  while (someComplexOperationIsOnGoing()) {
   checkRestart();
  }
 }
 catch (RestartException re) {
  startOperation();
 }
}

void checkRestart() {
 if (shouldRestart()) {
  throw new RestartException();
 }
}

This incomplete code sample is meant to show a case where an exception is not an error. 此不完整的代码示例旨在显示异常不是错误的情况。 This is not always best practice; 这并非始终是最佳做法。 but it is used in some cases where the intent is to interrupt the control flow deep in the program (such as redirecting the page in a web framework, when responding to an HTTP request) and return control to a higher-up level of the stack. 但是它在某些情况下用于中断程序深处的控制流(例如,在响应HTTP请求时重定向Web框架中的页面)并将控制权返回到更高级别的堆栈。 The term exception refers to the mechanism which interrupts the program. 术语“ 异常”是指中断程序的机制。

In java, there is an Exception class which encapsulates this behavior. 在Java中,有一个Exception类封装了此行为。 The Error class also interrupts the control flow in the same way as an Exception; Error类也以与Exception相同的方式中断控制流。 but it is reserved only for serious, unrecoverable problems that happen at runtime. 但它仅保留用于在运行时发生的严重的,不可恢复的问题。 It is used, for example, when the JVM runs out of memory and can't create new objects. 例如,当JVM内存不足并且无法创建新对象时,将使用它。

Exception is something more of an unexpected flow that can be handled. 异常更多是可以处理的意外流。 Compile time error is more like invalid code..so code doesn't even compile.. Hence term "error" since it denotes more serious problem which has to be fixed. 编译时错误更像是无效代码。.so代码甚至都无法编译。.因此,“错误”一词表示必须解决的更严重的问题。

Compile time errors are the result of the inability of the software to be created as it is instructed. 编译时错误是由于无法按照指示创建软件而导致的。 For instance: 例如:

String myString = new ButtonEvent();

is a compile time error. 是编译时错误。 While an exception is something that is caught during software processing. 虽然异常是在软件处理期间捕获的东西。

try{
    while( file.readNextLine() != file.EOF ){
    }
}
catch( UnopenedException ex ){
}

Here, we've made the assumption that the file could be properly opened and had been. 在这里,我们假设文件可以正确打开并且可以打开。 The exception is for those "exceptional" cases where opening the file didn't occur. 例外是那些没有打开文件的“特殊”情况。

An exception is the specific name for an error that can be handled within the logic of your software. 异常是可以在软件的逻辑内处理的错误的特定名称。 An error is simply that, a typo or just plain wrong code. 一个错误就是错别字或简单的错误代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM