Link to home
Start Free TrialLog in
Avatar of jkavx
jkavx

asked on

InterruptedException not caught

In the code below, the thread is being interrupted, but the InterruptedException is not being caught.  This is legacy code that has been in use for at least 6 years.  It was last tested about 2 years ago, at which time the InterruptedException was being handled.  At some point, it stopped working.  Because timeouts are rare, it hasn't been noticed.  The only changes would have been changing to Java 1.4, and upgrading WebLogic servers.  Does anyone understand why this InterruptedException would not be caught?

public class SomeClass implements Runnable, AbcTimerListener {
    public void timerExpired(AbcTimerEvent event) {
        event.getThread().interrupt();
    }
    public void run() {
        try {
            int iTimeout = 3000;
            AbcTimerManager.getInstance().register(this, iTimeout);
            try {
                execProcAndMapResult();
            } catch (Exception ex) {
                // should catch InterruptedException here
            }
            AbcTimerManager.getInstance().unregister(this);
 
Details from the log indicate that the timeout is being handled, the thread is interrupted, but we never enter the catch block in the run() method.  The log also indicates an uncaught InterruptedException.

|08/01/2007 18:20:39:713 AbcThreadPool.027|AbcTimerManager|D|
   Registered listener 'com.xxx.SomeClass' to be interrupted in 3000 milliseconds.
|08/01/2007 18:20:39:772 AbcSingletonThreadPool.000|com.xxx.AbcTimerManager|D|
   Interrupting listener: com.xxx.SomeClass
08/01/2007 18:20:39:871 AbcThreadPool.027|com.xxx.SomeClass $AbcExecuteThread|E|Thread in
    AbcThreadPool had uncaught exception.
     java.lang.InterruptedException

jkavx
Avatar of asood314
asood314

Sounds like the exception is occuring in the outer try block.  Check your catch statement for that block.
Avatar of jkavx

ASKER

For the sake of brevity, I omitted the full code, but the outer try block would also catch the exception.  It's not being caught there, either.
Where does the stack trace say the exception is occuring?
Avatar of jkavx

ASKER

There is no stack trace.  The thread interruption has no effect.  The code in the run() method is not interrupted and runs to completion.  Debugging output in the timerExpired method confirms that the method is invoked and that "event.getThread().interrupt()" executes cleanly.  This item in the log indicates that there was an uncaught InterruptedException:

08/01/2007 18:20:39:871 AbcThreadPool.027|com.xxx.SomeClass $AbcExecuteThread|E|Thread in
    AbcThreadPool had uncaught exception.
     java.lang.InterruptedException
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jkavx

ASKER

It's possible that the message in the log about uncaught "InterruptedException" is deceptive.  The code may be checking the status of the thread and writing the message because the thread's status is interrupted.

But this is code that has worked in the past.  So I have 2 questions:  what determines whether thread.interrupt() throws an InterruptedException, and did something change in Java 1.4 with regard to this?
> what determines whether thread.interrupt() throws an InterruptedException

whether the method being calleed when interuptteed detects or throws it or not.
eg. sleep() will, but most won't Its up to the application to check for interrupts itself.

> and did something change in Java 1.4 with regard to this?

not that I can remember, but it was a long time ago
Avatar of jkavx

ASKER

The timeout being checked for involves the execution of a stored proc.  So there's no way to check the status of the thread since there's a single line of code that's executing:
   ResultSet rs = stmt.executeQuery();
The timerExpired() method needs to stop this thread. Is there an alternative to thread.interrupt()?  I'm trying thread.destroy(), but get a NoSuchMethod error.
if the method is not interuptable then theres nothing u can do