• 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

Compile time vs Runtime errors

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I am confused as to when a piece of code will throw a compile time or runtime error.
For eg, this code there is no compiler error.Instead it gives a runtime error.
If a call to wait/notify must be within synchronized code, then why does it not complain at compile time. Is it not syntactically incorrect?

I am going wrong with most of these kinds of questions.
Can someone help me understand the difference between the 2 error cases, especially in more subtle cases ( I know when the syntax is wrong for a statement)?

Thanks
Sharda
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Originally posted by Sharda:
If a call to wait/notify must be within synchronized code, then why does it not complain at compile time. Is it not syntactically incorrect?


I think it compiles because it is possible that any other thread (not mentioned in current compilation unit) via a synchronized method could be calling this method. To demonstrate that I have just added one more layer of call in your code, and it compiles and runs fine...
 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


public class IllegalMonitorStateException
extends RuntimeException
Thrown to indicate that a thread has attempted to wait on an object's monitor or to notify other threads waiting on an object's monitor without owning the specified monitor.


The thread needs to own the object's monitor before making a call to funtions like wait, notify, notifyall as stated above. Since it's a unchecked exception you don't get a compile time error but a runtime one.
 
Shishio San
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to put clear-cut.


In Java terminology, a monitor (or semaphore) is any object that has some synchronized code.

 
knowledge is the difference between drudgery and strategic action -- tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic