e下演員具有能夠覆蓋的異常處理功能:
override def exceptionHandler = {
case e: Exception =>
println(e.getMessage())
}
每當一個異常的演員提出通常會導致它終止 - 在exceptionHandler
部分功能被應用到例外。
編輯
,出現異常過濾器:
class ExceptionalActor extends Actor{
def act() {
loop {
react {
case "bad" => throw new NoSuchFieldException("Bad Message")
case "impossible" => throw new Exception("Impossible Exception")
case m => println("non-bad message " + m)
}
}
}
override def exceptionHandler = {
case e: NoSuchFieldException => println("handled " + e.getMessage())
}
}
object Tester extends App {
val eActr = new ExceptionalActor
eActr start
eActr ! "any message1"
eActr ! "bad"
eActr ! "any message2"
eActr ! "impossible"
eActr ! "any message3"
}
生產:
non-bad message any message1
handled Bad Message
non-bad message any message2
[email protected]: caught java.lang.Exception: Impossible Exception
:
和演員死亡。
ref:Actors in Scala
很酷!這正是我所期待的。我期待在亞馬遜上看到這本書。謝謝! – 2011-04-29 06:50:10