i am in big problem, i have no idia about how to decide whether a program will give compilation errror or runtime error.can anybody give me guidelines to solve such type of problems just seeing the code, without executing on the machine...as in many exams we people face this problem. --- any tutorial, guidelines, suggetions appriciate me. Please help me guys..
The compiler will try its best to detect errors during compilation, eg
since Integer can never be a double, then the compiler detects this obvious mistake and flags a compilation error.
A runtime errors are errors that is impossible for the compiler to detect at compile time, eg.
may or may not throw a runtime cast exception depending on the type of o (if its not a String, then the program would halt and the vm will flag a class cast exception). There is no way that the compiler could anticipate the type of o during compilation time. Hope this helps.
If you get an error when you invoke javac to compile the code, or when you still get an error from the IDE after saving the code (for example Eclipse does), then that's a compilation error. The code cannot be compiled and therfore cannot be executed.
If the code compiles and runs, but throws an exception while running, no matter which exception, then it's a runtime error.
Compilation vs. Runtime is always a tricky topic, and I think its actually one of the points of SJCP certification that really distinguishes experienced programmers vs. novice programmers.
When asking if something will generate a 'compile time' error, what the question is essentially asking is: does this code comply with the Java syntax rules? If it does not, it will not compile.
With runtime errors, the question is really asking "when this runs, is there some logical error that will trigger a runtime exception, like a NullPointerException, ClassCastException or an ArrayIndexOutOfBoundsException."
In my personal experience, it's best to write code that both compiles, and does not cause any runtime exceptions, but others may have different experiences.
it's best to write code that both compiles, and does not cause any runtime exceptions
An excellent motto!
My advice to those studying for the exams and troubled by this question is: Write write write ... etc. LOTS and lots of little programs to exercise all of the aspects of the language. EVERY time you get a compile time or runtime error - do not rush - figure out exactly what happened and what the fix is. Nothing will set the language in your head like this exercise. Book study time simply can not substitute for this. If you don't get enough errors, try to cause some.
Joined: Oct 08, 2006
If you don't get enough errors, try to cause some.
Joined: Feb 09, 2003
Yes, syntax error is a type of compile time error, but the compiler does more than just syntax checking, it also does some static checking to catch obvious errors.
although syntatically correct, the compiler will flag a compile time error because a wasnt assigned a value before being used (obvious bug).
Originally posted by William Brogden: Write write write ... etc.
I concur, this is the effective response to the poster's unstated, but inferred question. This, today,is how I develop useful code.
Have a real world problem to solve
Insure that the problem lends it's self to computational devices.
code/compile/test;code/compile/test;code/compile/test Runtime errors are stated to be a class in Java, this may mislead some students to conclude Java catches logic errors.
"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Joined: Aug 02, 2006
you all helped me lot.
and dear syamsul thanks a lot, and you are very near to my problem
"although syntatically correct, the compiler will flag a compile time error because a wasnt assigned a value before being used (obvious bug)."
what i am thinking, that people always said compiler check only for syntactical errors, and you explore the next point undefined local variable also give compile time error.. some unanswered problem
1. what about signature of method (when wrong, will give compile time or runtime error) 2. trying to override a final method? 3.and in some cases when we implement rumtime polymorphism, it will also give compile time error ---- is there any hard and fast rule??? will be waiting for your replies.....
Author and all-around good cowpoke
Joined: Mar 22, 2000
Tch tch - you have apparently ignored my advice.
Write write write - some test programs that exercise your "unanswered problems" and see what happens.
Joined: Aug 02, 2006
hi william sir;
i always used the technique you advised me, but at the time of exam we do'nt have such type of space, so i just wanted to know are thing that compiler checks
Joined: Feb 09, 2003
The way i usually think about compile time and runtime error is to figure out the information available to the compiler during compile time.
1. what about signature of method (when wrong, will give compile time or runtime error)
Lets look at an example
The compiler has all the information to conclude that there is not enough information to disambiguate the two methods. This error will be captured by the compiler.
trying to override a final method?
Same case. If a method has been tag with a final modifier, the compiler will immediately detect the error of trying to override it.
3.and in some cases when we implement rumtime polymorphism, it will also give compile time error
With polymorphism, sometime the type of an object could only be determined at run time.
Lets say int A is the user's input. There is no way the compiler could tell the type of o in advance. If A is not 1 then it will be a runtime error. [ October 26, 2006: Message edited by: Syamsul Hussin ]
compiler will raise an error if you try to overrite it with
public void methodA() throws Exception
and you can conclude this rule because as Hussin said, compiler will think at compile time that he is calling methodA
parentRef.methodA(); // compiler know nothing about late binding ( overriding) // It will think that you are trying to call methodA() in parent // class, and catching the thrown exception with uncompatible one // IOException is uncompatible with Exception because you cannot down // cast Exception to IOException (in a catch), so It will //raise an error.
applying this rule Syamsul Hussin , "Think about what compiler knows" what will happen if you try to override a method with weaker access modifier, but remember that, private methods are never inherited.
Another thing If you are trying to be certified on scjp, you cannot do it and pass without alot of code as William Brogden said.