aspose file tools*
The moose likes Java in General and the fly likes How to know whether Compile time or runtime error!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to know whether Compile time or runtime error!! " Watch "How to know whether Compile time or runtime error!! " New topic
Author

How to know whether Compile time or runtime error!!

Anuragk kushwaha
Ranch Hand

Joined: Aug 02, 2006
Posts: 51
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
Please reply me friends..........
Syamsul Hussin
Ranch Hand

Joined: Feb 09, 2003
Posts: 59
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
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Please don't post the same question to more than one forum. See here for an explanation of why I just deleted the other copy of this thread.


[Jess in Action][AskingGoodQuestions]
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

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.

-Cameron McKenzie
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
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.

Bill
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
If you don't get enough errors, try to cause some.
Syamsul Hussin
Ranch Hand

Joined: Feb 09, 2003
Posts: 59
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
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
    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: 12835
        
        5
    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
    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
    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
    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

    public void methodA() throws IOException

    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.

    }catch (IOException e) {

    }

    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.


    SCJP 5
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to know whether Compile time or runtime error!!