user327663
user327663

Reputation:

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 ?

I mean to say that we never say compile-time exception.
We tend to say it as compile-time error.

Is there any specific reason for the same ??
Any suggestions are welcomed....

Thanks !

Upvotes: 8

Views: 3247

Answers (6)

RMorrisey
RMorrisey

Reputation: 7739

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. The term exception refers to the mechanism which interrupts the program.

In java, there is an Exception class which encapsulates this behavior. The Error class also interrupts the control flow in the same way as an 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.

Upvotes: 3

Inv3r53
Inv3r53

Reputation: 2959

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.

Upvotes: 2

vodkhang
vodkhang

Reputation: 18741

Exception in java is really different than compile error. 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.

Upvotes: 4

wheaties
wheaties

Reputation: 35970

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.

Upvotes: 1

danben
danben

Reputation: 83250

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.

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!

Upvotes: 11

blissapp
blissapp

Reputation: 1370

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.

Upvotes: 0

Related Questions