• 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

Compiler Error Vs. Runtime Exception

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Mike Cunningham
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the explanation Viki.
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry guys, I made a mistake! I found out the reason now! I am cool.
 
Jim Hall
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
A feeble attempt to tell you about our stuff that makes us money
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic