6

如何从与 #&& 等结合的外部进程中捕获异常?

scala> import scala.sys.process._     
scala> try{ "throw " ! }catch{ case e: Exception => }
res1: AnyVal = ()
scala> try{ "throw " #&& "ls" ! }catch{ case e: Exception => }
Exception in thread "Thread-10" java.io.IOException: Cannot run program "throw": error=2, No such file or directory
4

1 回答 1

3

你已经这样做了。尝试

try {
 val x = "throw" #&& "ls" !
} catch {
 case x => println("caught")
}

只是将!异常记录到控制台,当你在 REPL 中看到它时有点混乱,但它不会崩溃。

于 2012-09-03T12:29:14.233 回答