• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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.

 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic