wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please explain how does this code reaches the Exception code and prints 20 GOTO 30 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 "Please explain how does this code reaches the Exception code and prints 20 GOTO 30" Watch "Please explain how does this code reaches the Exception code and prints 20 GOTO 30" New topic
Author

Please explain how does this code reaches the Exception code and prints 20 GOTO 30

Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174


Doubt in this for how the exception is thrown, how does it print 20 GOTO 30
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Instead of catching Exception try catching the specific Exception type that is thrown by this code. What must you do before calling wait() ? Go through the thread topics a little more and you will understand the reason behind the exception.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
sebastian tortschanoff
Ranch Hand

Joined: Mar 19, 2009
Posts: 68
The wait() method must be called from synchronized context otherwise a IllegalMonitorStateException might be thrown, when wait() is invoked on an object.

The code in main() is lega,l since Thread-Class implements Runnable-Interface.

The only way to acquire the Monitor of an Objext is to synchronize on it. Otherwise an Exception will be thrown, when wait(), notify() or notifyAll() is invoked on an object from non-synchronized context.

A thread becomes the owner of the object's monitor in one of three ways:

* By executing a synchronized instance method of that object.
* By executing the body of a synchronized statement that synchronizes on the object.
* For objects of type Class, by executing a synchronized static method of that class.

None of this happens in this example so an exceptions will be thrown.


Power from within.

Failed SCJP 2 times :-(
Silke Bader
Greenhorn

Joined: Jul 04, 2009
Posts: 3
Method wait() is only valid in a synchronized method.
Since there is no synchronization an error is thrown and catched. Hence "20 GOTO 30" is printed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please explain how does this code reaches the Exception code and prints 20 GOTO 30