pythonic pythonic - 26 days ago 7
Java Question

How can I catch an interrupted java process in Scala

So, inside my Scala program, I run some Java programs using Scala's process package. Sometimes those Java programs are terminated due to an error. And when that happens they exit with some message, including the stack trace. Now, my question is how can I catch those errors as exceptions. If I simply use

catch { case e: Exception => ... }
, those errors are not caught. I though about using
RunTimeException
or
InterruptedException
, but wouldn't
Exception
include those anyway? So, I thought that wouldn't still solve the problem.

By the way, I launch those programs using something like shown below.

"java -jar programsName parameters ...".!

Answer

If you run it with the !! method instead, it will return the output or throw an exception in case of failure as you expect:

Starts the process represented by this builder, blocks until it exits, and returns the output as a String. Standard error is sent to the console. If the exit code is non-zero, an exception is thrown.

While the ! method just returns the exit code.