• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to differentiate between compile errors and runtime errors.

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Friends,
I have a difficulty in understanding in locating compile error,compliler objection,runtime exception being thrown and runtime error.
for example
public boolean testAns(String ans,int n)
{
boolean rslt;
if(ans.eualsIgnoreCase("YES") && n>5)
rslt=true;
return rslt;
}
what will be the result of trying to compile class and execute the testAns method with inputs no and 5?
1)a runthime exception will be thrown in the testAns method
2)A result of false will be returned
3)a compiler error will prevent compilation
4)a result of true will be returned
here the answer is 3.But I just want to know how to find it exactly.How to differentiate between compile errors and runtime errors.?compile time exceptions and runtime exceptions?I am always confused finding the correct option.
thanks.
Preeth

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To your question,
As far as i understand,
During compilation your program code is converted to machine code which is your binary Machine code only understood by your computer. So if there are any class errors your console prompts you a compilation error.
On the other hand at run time,
may have something to do with your CLASSPATH, or main method.
(*PLEASE CORRECT ME IF IM WRONG)
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Preeth,
It's a difficult question. Any kind of answer can be proven incomplete. Howerver, let me try...
To nail down the compiler error(s), you will have to think like a compiler
Some( but not all ) checks are listed below

    * Check scope and accessibility of variables/method calls.
    * Check improper syntax.
    * Check out of range assignments.
    * Check incompatibile references.
    * Check overloading/overriding to make sure they confirm to rules.
    * Check to see if all checked exceptions are handled/declared.
    * Check and resolve all references to "external" entities( imported
    classes, interfaces etc.)

    As you can see, quite a lot of checks are done at the compile time to help you( the programmer ) fix the error.
    Runtime errors are usually caused by something that is syntactically right, but semantically wrong. For example, consider the statement-
    int i = myIntArray[someIndex]
    Now, for this statement to be syntactically correct, the following conditions should be true

      * myIntArray should be declared as int[].
      * someIndex should be declared as int.

      As long as these two conditions are satisfied, your code compiles fine. However, at Runtime, someIndex might have a value which exceeds the size of the array in which case you will get ArrayIndexOutOfBoundsException.
      Let's say you created another program which uses I/O streams. You unconsciously created the stream, closed it, and then try to read it! For the compiler, it is perfectly okay, because it meets all the strict syntax checkings. However, it sure will bomb at runtime when the JVM tries to read from a closed stream!
      Hope this helps a little.
      Ajith
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Preeth,
The first thing you should do is to write a small complete program that contains the progrma fragment you have question.
Compile it and run it if it compiles clean. Compare the result with the options there, you know what is the correct answer. To help you start, here is the program that you can compile. It does have compilation error.
<pre>
class TestAnswer {
public static void main (String args [])
{
TestAnswer tst = new TestAnswer ();
tst.testAns ("no", 5);
}
public boolean testAns(String ans,int n)
{
boolean rslt;
if(ans.equalsIgnoreCase("YES") && n>5)
rslt=true;
return rslt;
}
}
</pre>
 
reply
    Bookmark Topic Watch Topic
  • New Topic