views:

104

answers:

6

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 !

A: 

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.

blissapp
Java har runtime *errors* as well.
aioobe
Runtime errors are what we call unhandled exceptions
blissapp
Perhaps you call them that way, the folks at Sun don't. (c.f. javadoc for java.lang.Error).
meriton
No, RuntimeExceptions are what we call unhandled exceptions ...
EJP
+6  A: 

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!

danben
+1  A: 

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.

wheaties
+4  A: 

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.

vodkhang
+2  A: 

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.

daedlus
+3  A: 

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.

RMorrisey
Addendum: And since a compile time error is unrecoverable, it makes more sense to call it error than exception.
meriton