Extracting a process's exit code in the case of ThreadInterrupted
Asked Answered
N

1

7

I have just created a process through an exec() call and I am now using its .waitFor() method. I need to catch an InterruptedException but I am not sure what I should place in the catch code block. I would like to receive the exit code but I won't if the current thread is interrupted. What should I do to get the exit code out of the process if the thread is interrupted?

Example:

import java.io.IOException;


public class Exectest {
public static void main(String args[]){
      int exitval;

      try {
        Process p = Runtime.getRuntime().exec("ls -la ~/");
        exitval = p.waitFor();
        System.out.println(exitval);
    } catch (IOException e) {
        //Call failed, notify user.
    } catch (InterruptedException e) {
        //waitFor() didn't complete. I still want to get the exit val. 
        e.printStackTrace();
    }

}
}
Nameless answered 28/5, 2011 at 12:20 Comment(0)
T
10

If I were you, I'd put this into the catch block:

p.destroy();
exitval = p.exitValue();

Since your thread has been interrupted, something has gone wrong. destroy() will forcibly terminate the process, and then exitValue() will give you the exit value (which should be an error code since it's been terminated).

More http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Process.html

Tattler answered 28/5, 2011 at 12:24 Comment(7)
Is it necessarily the case that an InterruptedException signals an error? I thought it might be something that just happens for any number of reasons.Nameless
Nope...it's not necessarily an error. It just means that the current thread has been interrupted for whatever reason. It's up to the programmer to decide what to do with it. Generally, though, it's a good idea to stop what you're doing and return. More here: download.oracle.com/javase/tutorial/essential/concurrency/…Tattler
I think this answer is incorrect. Getting an InterruptedException implies that the current thread was interrupted. It tells you nothing about the status of the process. See ibm.com/developerworks/java/library/j-jtp05236/index.htmlExecrative
I didn't say that anything went wrong with the process. Rather that it's generally better to clean up and go home when there's an InterruptedException and how to do just that with a Process object.Tattler
Ok, but will an InterruptedException ever even be generated? From what I've been reading it looks like an event that is completely up to the programmmer to produce. when Thread.interrupt() is called.Nameless
Honestly, probably not. But whenever you're dealing with threads, it's better to be prepared to deal with interruptions because they can come from a variety of sources. Here, the program is simple enough that you could just surround your p.waitFor() with a while loop and ignore any InterruptedException that pops up. In a more complex program, that would be less wise. It's one of those use your best judgement sorts of cases. ;)Tattler
Nice! I've been trying to figure out the different contexts this exception might occur in. On the one hand it looks like if I have written the code myself then I can be sure when a thread is interrupted (after all I will have written thread.Interrupt()!) But if my code is going to be used by others (i.e I'm writing a library) then someone else may call thread.Interrupt() in their code and I'll have to account for that case. So I guess in general if you own the code completely you don't have to deal with this error, otherwise budget the possibility that others might generate an interrupt!Nameless

© 2022 - 2024 — McMap. All rights reserved.