File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exceptions thrown by wait(). Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exceptions thrown by wait()." Watch "Exceptions thrown by wait()." New topic

Exceptions thrown by wait().

geetha nagarajan
Ranch Hand

Joined: Jul 13, 2001
Posts: 94
Question from Javacaps sample test :2.
Select all the exceptions thrown by wait() method of an Object class, which you can replace in the place of xxx legally?

The answer is given as :
catch ( InterruptedException ie) {}
catch ( IllegalArgumentException il ) {}
catch ( IllegalMonitorStateException im ) {}
Does wait() throw IllegalArgumentException ?
I thought wait() throws only InterruptedException and IllegalMonitorStateException.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
IllegalArgumentException is a runtime exception. Therefore, a method that throws it need not declare it and you can catch it at any time.

SCJP Tipline, etc.
Swati Gupta
Ranch Hand

Joined: May 28, 2002
Posts: 106
The parameter to wait() method is either (long msec) or (long msec, int nanosec)
If this parameter is not proper (as in this case it is -ve) the wait() methos will throw IllegalArgumentException.
Suresh Thota
Ranch Hand

Joined: Sep 24, 2003
Posts: 152
Hi all,
Resurfacing a old thread. Please see the question above. This is from java caps mock - 2.
Though IllegalArgumentException & IllegalMonitorStateException are legal exceptions thrown by the wait() method, InterruptedException is a checked exeption which comes under handle or declare rule. But as only one of the exception's can replace XXX, it should be InterruptedException. isn't it? I think the answer should be only InterruptedException. someone please clarify.

SCJP 1.6
Kurankyi Dadson

Joined: Mar 12, 2004
Posts: 14
The question says 'legally' not 'appropriately' so the best way to verify is to insert the code and see waht the compiler says. I should imagine that it will permit even the unchecked exceptions.
Kurankyi Dadson

Joined: Mar 12, 2004
Posts: 14
Just tried it. You can catch the other two exceptions in addition but MUST catch the InterruptedException. So the one Interrupted will appear to be the right answer
I agree. Here's the link:
subject: Exceptions thrown by wait().
It's not a secret anymore!