Possible Duplicate:
Runtime vs Compile time
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.
Possible Duplicate:
Runtime vs Compile time
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.
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:
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:
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.
To see if your code doesn't compile, try to compile it. The compiler will complain.
To see if your code contains runtime errors, write unit tests.
Practically every line of code may in theory throw a runtime exception and there is no such thing as "throwing a compile-time error". I believe you have mixed up compiler errors with runtime exceptions.
Anyway, the compiler errors are easy: compile it and see. There is never any "may" about them: a line of code definitely will, or will not, produce a compiler error.
As for a runtime error, the best you can do is statically analyze and guess whether a line of code will result in an error. Some cases are dead obvious, such as throw new RuntimeException()
or null.toString()
, but most aren't and require great experience to analyze without executing.
The best way would be to compile it, then most of the compilers will point out all of the compile-time errors. Run-time errors on the other hand require greater effort from the developer.
Some compilers inform the user of the exceptions that maybe thrown, but the best way to find if a particular line of code contains run-time is run them with different inputs and observe the results(i.e use Unit Tests, sanity tests etc).
When you compile your code, you will see wether there's a compile time error. Running the code might show runtime errors.