This week's giveaways are in the MongoDB and Jobs Discussion forums. We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line! See this thread and this one for details.
How to know whether Compile time or runtime error!!
Anuragk kushwaha
Ranch Hand
Joined: Aug 02, 2006
Posts: 51
posted
0
Hi Ranchers;;;
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..
Anuragk kushwaha
Ranch Hand
Joined: Aug 02, 2006
Posts: 51
posted
0
Please reply me friends..........
Syamsul Hussin
Ranch Hand
Joined: Feb 09, 2003
Posts: 59
posted
0
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.
----------------------------------<br />SCJP 1.4
Bauke Scholtz
Ranch Hand
Joined: Oct 08, 2006
Posts: 2458
posted
0
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.
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12265
1
posted
0
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.
If you don't get enough errors, try to cause some.
Syamsul Hussin
Ranch Hand
Joined: Feb 09, 2003
Posts: 59
posted
0
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.
eg.
although syntatically correct, the compiler will flag a compile time error because a wasnt assigned a value before being used (obvious bug).
Nicholas Jordan
Ranch Hand
Joined: Sep 17, 2006
Posts: 1282
posted
0
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."
Anuragk kushwaha
Ranch Hand
Joined: Aug 02, 2006
Posts: 51
posted
0
Hi Friends,
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.....
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12265
1
posted
0
Tch tch - you have apparently ignored my advice.
Write write write - some test programs that exercise your "unanswered problems" and see what happens.
Bill
Anuragk kushwaha
Ranch Hand
Joined: Aug 02, 2006
Posts: 51
posted
0
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
Syamsul Hussin
Ranch Hand
Joined: Feb 09, 2003
Posts: 59
posted
0
anuragk,
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 ]
Costa lamona
Ranch Hand
Joined: Sep 24, 2006
Posts: 102
posted
0
Hi
in addition to what Syamsul Hussin said there are another rules about overriding some of them apply only in java 5 like covaraint return
others like
1- you never Override a method and throw a wider checked exception like this
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
try {
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.