aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Compiler Error Vs. Runtime Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Compiler Error Vs. Runtime Exception" Watch "Compiler Error Vs. Runtime Exception" New topic
Author

Compiler Error Vs. Runtime Exception

Mike Cunningham
Ranch Hand

Joined: Nov 14, 2000
Posts: 129
For exam purposes, is there any clearcut documentation on when to expect a compiler error vs. a runtime exception? I find that this is an area that I get messed up on when taking pop quizzes for SCJP.
Thanks,
Mike
Vikrama Sanjeeva
Ranch Hand

Joined: Sep 02, 2001
Posts: 756
I don't think that there is any documentation.But i believe that it is totally conceptual rather than learning by heart!.You must know how much JVM compiler is smart.This could be achieved by more coding & book reading.
For e.g. the type of reference variable is known at compile time but the class of an object referenced by such a varaiable cannot be known until runtime.
Bye.
Viki.
------------------
Count the flowers of ur garden,NOT the leafs which falls away!


Count the flowers of your garden, NOT the leafs which falls away!
Prepare IBM Exam 340 by joining http://groups.yahoo.com/group/IBM340Exam/
Mike Cunningham
Ranch Hand

Joined: Nov 14, 2000
Posts: 129
The particular example that stumped me tonight was the following:

I felt the compiler should be able to construct the portion of the array that is defined and blow up on the following line where a reference to the second dimension is not yet created. Instead it gives a NullPointerException at runtime.
I guess a reasonable strategy would be to study the potential runtime exceptions.
Vikrama Sanjeeva
Ranch Hand

Joined: Sep 02, 2001
Posts: 756
WOW!!
When i was given the same question by my instructor i got the right answer ....
I would like to explain this here:
Explanation
At compile time when compiler comes to this line,
Log[][] theLogs = new Log[3][];
It creats an array theLogs of type Log with length 3.At runtime when JVM comes to this line in order to print,
System.out.println(theLogs[2][0]);
It goes to 3rd row and then finds for coloumn which is ZERO,& this is the stage where JVM fails to point the exact two dimension location & throws NullPointerException

Therefore i said in my first post that its is conceptual!
Bye.
Viki.

------------------
Count the flowers of ur garden,NOT the leafs which falls away!
[This message has been edited by Vikrama Sanjeeva (edited December 10, 2001).]
Mike Cunningham
Ranch Hand

Joined: Nov 14, 2000
Posts: 129
Thanks for the explanation Viki.
Jim Hall
Ranch Hand

Joined: Nov 29, 2001
Posts: 162
Some things you'd expect the compiler to be able to figure out at compile time.
Another one pertains to Threads. If you make a call to the wait(), notify(), notifyAll() methods when not in synchronized code, it will throw a runtime IllegalMonitorStateException. Can't the compiler see this?
Vinny Chun
Greenhorn

Joined: Dec 06, 2001
Posts: 24
I don't get it. When I compiled the example code in the above, I got an compile error. It was impossible running the class and how could you get a runtime error?
Please help!
Vinny Chun
Greenhorn

Joined: Dec 06, 2001
Posts: 24
Sorry guys, I made a mistake! I found out the reason now! I am cool.
Jim Hall
Ranch Hand

Joined: Nov 29, 2001
Posts: 162
The code above refers to a Log class. You would need to add the following code to the example.
<code>
class Log {}
</code>
Vikrama Sanjeeva
Ranch Hand

Joined: Sep 02, 2001
Posts: 756
Originally posted by Jim Hall:
Some things you'd expect the compiler to be able to figure out at compile time.
Another one pertains to Threads. If you make a call to the wait(), notify(), notifyAll() methods when not in synchronized code, it will throw a runtime IllegalMonitorStateException. Can't the compiler see this?

Jim plz. consider the following code.Here wait() is called in asynchronous method & no IllegalMonitorState Eception is thrown.


------------------
Count the flowers of ur garden,NOT the leafs which falls away!
Uma Viswanathan
Ranch Hand

Joined: Jun 14, 2001
Posts: 126
wait() is called in asynchronous method bMethod();
But the method bMethod() is called from the "synchronized" method aMethod().
So, the thread has to acquire the lock to execute the aMethod. It will hold the lock until it returns from that method. Since you are calling bMethod() from aMethod(), the thread will have lock when it executes the bMethod(). That's why you are not getting the Runtime exception.
If you remove the keyword synchronized from aMethod(), then you will get Runtimeexception
 
jQuery in Action, 2nd edition
 
subject: Compiler Error Vs. Runtime Exception