Re: Better way to do error handling here?
ram@zedat.fu-berlin.de (Stefan Ram) wrote in news:error-catching-
20091019222539@ram.dialup.fu-berlin.de:
laredotornado <laredotornado@zipmail.com> writes:
phpProcess = Runtime.getRuntime().exec(phpNormalizerCommand);
from a termianl . Is there some way after first ilne of the code above
that I could trap for this error and stop my program if I detect such
an error?
Usually, the following pattern might work:
try
{ phpProcess = Runtime.getRuntime().exec(phpNormalizerCommand);
if( phpProcess == null )handleNullProcessResult();
else use( phpProcess ); }
catch( final java.lang.Throwable throwable )
{ handle( throwable ); }
After you found the precise type of the error, you should
restrict the catch clause to catch only this.
(I have not looked up the docs and therefore might have
missed something.)
The javadocs are your friend:
"Starting an operating system process is highly system-dependent. Among the
many things that can go wrong are:
The operating system program file was not found.
Access to the program file was denied.
The working directory does not exist.
In such cases an exception will be thrown. The exact nature of the
exception is system-dependent, but it will always be a subclass of
IOException."
In fact, the javadocs provide this at the begining of the definition for
exec:
public Process exec(String command) throws IOException
The original poster should know this and omitted some important code in the
snippet that was posted.
It might have been simpler to modify phpNormalizerCommand and test the
result than it was to post to the newsgroup.
I tested on WindowsXP using JDK 1.6.0_14 using
the following test program:
package testpack1;
import java.io.IOException;
public class ExecNonExistentProgram {
public static void main(String[] args) {
try {
Runtime.getRuntime().exec("nonexistent");
} catch (IOException e) {
e.printStackTrace();
}
}
}
which generates:
java.io.IOException: Cannot run program "nonexistent": CreateProcess error=
2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
at java.lang.Runtime.exec(Runtime.java:593)
at java.lang.Runtime.exec(Runtime.java:431)
at java.lang.Runtime.exec(Runtime.java:328)
at testpack1.ExecNonExistentProgram.main
(ExecNonExistentProgram.java:15)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot
find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
at java.lang.ProcessImpl.start(ProcessImpl.java:30)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
... 4 more
which then suggested:
package testpack1;
import java.io.IOException;
public class ExecNonExistentProgram2 {
public static void main(String[] args) {
try {
(new ProcessBuilder("nonexistent")).start() ;
} catch (IOException e) {
e.printStackTrace();
}
}
}
which generates:
java.io.IOException: Cannot run program "nonexistent": CreateProcess error=
2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
at testpack1.ExecNonExistentProgram2.main
(ExecNonExistentProgram2.java:6)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot
find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
at java.lang.ProcessImpl.start(ProcessImpl.java:30)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
... 1 more