[2269] in java-interest

home help back first fref pref prev next nref lref last post

Re: thread.sleep and java.lang.InterruptedException

daemon@ATHENA.MIT.EDU (Jim Graham)
Wed Sep 27 15:55:31 1995

Date: Fri, 22 Sep 1995 18:39:45 -0700
From: flar@bendenweyr.Eng.Sun.COM (Jim Graham)
To: Janet.Coursey@Central.Sun.COM
Cc: java-interest@java.Eng.Sun.COM


> In the JDK prebeta1 , I get this compiler error:
> 
> 	javac Foo.java
> 	Foo.java:11: Warning: Exception java.lang.InterruptedException must be caught, or it must be  
> declared in throws clause of this method.
> 	        thread.sleep(150);
>     	   	            ^
> But when I do what it suggests I get this compiler error:
> 	javac Foo.java
> 	Foo.java:8: Invalid exception class java.lang.InterruptedException in throws clause. The  
> 	exception must be a subclass of an exception thrown by void run() from interface  
> 	java.lang.Runnable.
> 	public void run() 
> 
>             ^

As the first error message says you can do one of 2 things:

	1) Catch the exception

	2) Declare the exception as something you throw.

You tried the second and it doesn't work since you aren't allowed to
throw an exception from a run() method.  So, you should instead do the
first suggestion - catch the exception and handle it gracefully.  Try:

	run() {
	    ...
	    try {
		Thread.sleep(150);
	    } catch (InterruptedException e) {
		return;
	    }
	}

				...jim
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post