Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compiler error Vs Runtime Exception

 
jibs parap
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,
Is there any general explanation about what compiler looks for against JVM(at run time).
In many of the questions, we tend to know where its going wrong, but can't predict whether its a compiler or runtime error.

Thanks
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jibs,

Its not like that. You can easily distinguish whether it is compiler error
or code may throw runtime exception.

Compiler error:
1- Syntax error
2- Code that may throw checked exception, is not enclosed inside the try
block or the exception is not declared. This is the case the compiler
complaints.

Runtime exception:
Examples:




Thanks,
 
jibs parap
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think its too general an explanation.

If you take the eg:
1. class Dims {
2. public static void main(String[] args) {
3. int[][] a = {{1,2,}, {3,4}};
4. int[] b = (int[]) a[1];
5. Object o1 = a;
6. int[][] a2 = (int[][]) o1;
7. int[] b2 = (int[]) o1;
8. System.out.println(b[1]);
9. }
10. }

Why its run time exception(at line 7) but not compile time error? How did ot get past the compiler? If the syntax says, 2D can't be assigned to 1D array, why didn't throw a compiler error?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this link for detailed discussion


Thanks,
 
jibs parap
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, that doesn't answer my query. I don't want the concepts to be cleared because Im clear of it. What Im not sure is why its a run time exception rather than compile time error?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



If the syntax says, 2D can't be assigned to 1D array, why didn't throw a compiler error?


It is matter of run time. At compile time it is not known, to what dimension
of array the ref variable o1 is pointing to. Arrays in java are simply
Object with capital O. Object ref variable can refer to array of any
dimension on the heap. At runtime o1 refers to 2D array, the JVM comes to
know the wrong assignment(casting). Compiler is innocent of all those
things.

In short, compiler can't find out the runtime behavior. It is what JVM is
for.

Thanks,
[ July 21, 2007: Message edited by: Chandra Bhatt ]
 
Anand Hariharan
Rancher
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your question is fundamentally no different from -

"Why cannot the compiler evaluate the expression

1/sin(3.14159265358979)

at compile time and fail because the expression evaluates to infinity?"

Basically, the compiler parses source code, ensures syntactical correctness against language grammar, etc. It does not actually evaluate the expressions.

In your program, your cast at line 7 subverts the type-check, and fools the compiler into believing that the types on both sides of the '=' are the same. You pay the price of cheating the compiler as a run-time exception.

- Anand
 
jibs parap
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, not clear still. If the the compiler sees things as Object with capital O, the following doesn't compile if compiler sees Me as just an object only.

class Test {
public static void main(String[] args) {
Test t1 = new Me();
}
}
class Me{
}

Sorry, I just want to have the explanations clearer which are applicable across all java compilations.
 
Abhinav Srivastava
Ranch Hand
Posts: 354
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A a;
B b;

If A is a superclass of B,
a = b; is valid at compilation time.

If B is a superclass of A,
a = b; is not valid at compilation time.
However, a = (a) b is valid as it defers the check to runtime. For objects of type lower than A having reference of type B can be safely cast to type A.

If A and B do not belong to the same hierarchy,
both a = b; and a = (a) b; are invalid at compilation time.
[ July 21, 2007: Message edited by: Abhinav Srivastava ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi Jibs,
Here IS-A fails. In case of object, arrays in java are object with capital
'O'.

See the code:



We are only able to assign Animal object to Object reference variable because there is IS-A relationship between Animal and Object as Animal
IS-A Object.

In your example it is not the case, that is why compiler complaints. There is no IS-A between Test and Me.
Test and Me are siblings, son of father Object of course.


Thanks,
[ July 21, 2007: Message edited by: Chandra Bhatt ]
 
jibs parap
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for replying. There are still some area thats not very clear. Replies were mostly example based; there are other scenarios where you would want to predict whether its compiler or runtime errors.

Sun has the final word on java. So Sun must have explained how compiler or JVM should behave somewhere. Does anyone know where I can find those specification.

Cheers
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So Sun must have explained how compiler or JVM should behave somewhere.
The The Java Language Specification

[ July 22, 2007: Message edited by: Manfred Klug ]
[ July 22, 2007: Message edited by: Manfred Klug ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jibs,

Come with any scenario that pinches your mind. Compiler does not have dynamic
behavior in the sense, it can't know for sure the details about the run
time of the program.

One more example I can tell you, that is downcasting. It is ok if you downcast
with explicit casting. Compiler is happy with that but at rum time when JVM
sees this is not OK, it throws ClassCastException.

See the code again:



If we change Line #1 to Animal a1 = new Dog(); then there wont be
ClassCastException at Line #2. Because Dog reference variable d1
can hold object of the Dog obviously.


I shown you some scenarios where you can determine, what may go wrong,
just seeing at code. Other situation may depend upon runtime circumstances
of the program, for instance IOException, FileNotFoundException,
NotSerializable exception etc. But you know the probability of exception
so you are forced to handle the checked exceptions by the compiler whereas
you are free in the case of unchecked exceptions.


Thanks,
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jibs,

I think your main problem is, that the samples are too small. You see the code and think that the error is obvious. Look at the following sample and ask yourself, how the compiler could know that worker.doStuff() returns a Cat.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic