Is there a rule of thumb to distinguish between Compiler Error Vs. Run-time error. Syntax errors obviously generate Compiler errors. Apart from Syntax errors, how can you say whether a particular group of code produces compiler or Run-time error ? Thanks.
My question was specific for Java certification exam. You know that a block of code is incorrect and is going to generate some errors. My question is: Is there a logical way, by which you can predict that this block of code is going to generate compiler error as opposed to run-time error. Thanks
Wellcome to the Ranch swamyarun Please read about our name policy and adjust your displayed name accordingly. Experience will tell you which exceptions are runtime or not. In the meantime consider if it is feasible to force a try clause, each time the exception could be arised in the program. For instance, a NullPointerException being a checked one would force to put all the method invocations within a try, or declare it in the method. Not feasible, thus, it is a RuntimeException. Also remember that certain actions will always imply such risk, that the exceptions they could arise are checked by the compiler for you not forgetting about them. Forgetting about managing them, I mean. That is the case of the actions that have to access the file system and the IOException. Now I see that my answer is not very related to your question. I will keep it anyway for the benefit of others. [ August 12, 2002: Message edited by: Jose Botella ]
There's many errors in semantics that the compiler can discover too. As you learn the language and compile code you are going to get some the compiler errors. Some are going to stick in your head. What I do is learn a bit from one of the well known Java books, code a bit, then I pick up the Java Language Specification and try to get a more deeper understanding of a topic. The JLS is full of "It is a compile time error to ...". No easy way to do it, like making money. -Barry
Joined: Jul 03, 2001
Distinguising between code that will compile, or not, and the one generating exceptions is of paramount importance for the exam. There isn't a unique rule for that. However while studying the Java language, the sooner someone is aware of the compiler error/runtime exception dichotomy, the sooner he will begin to really understand the language. That is, while examining a Java piece of code first think if the code compiles, and then if the code will execute. But do these in two different steps. For instance, the compiler checks for the existance and accesibility of all the expressions that access members of a class. In this case the "interpreter" has nothing to do with it. Casting is an area where distinguishing, when the compiler will complain -and when ClassCastException is thrown, requires a good understanding of what will compile, and if does so -the second step, what will run. I am sorry no magic wand for that. Each case must be studied separately.