我对此感到非常难过。这应该很简单,并且不受期望的影响。我查看了类似的SO线程,它们涉及延迟执行或其他与此无关的问题。
仅供参考,这是Scala 2.11.8。我正在调用Apache Kafka 0.10.1.1库:
def deleteTopic(zookeeperHostPort: String, topicName: String): Unit = {
try {
// the following log statement does execute. No surprise.
logger.info("deleteTopic1")
// If I uncomment the following simple `throw`, it is caught as expected.
// throw new java.lang.IllegalArgumentException("test")
// The following TopicCommand.main throws an exception that isn't caught.
TopicCommand.main(Array("--zookeeper", zookeeperHostPort, "--delete", "--topic", topicName))
// the following log statement doesn't execute
logger.info("deleteTopic2 (probably will not output)")
} catch {
case _: IllegalArgumentException => logger.info(s"Error deleting topic $topicName. It probably did not exist.")
case e: Exception => logger.info(s"Error (${e.getClass.getName}) deleting topic $topicName. It probably did not exist.")
}
// the following log statement doesn't execute
logger.info("deleteTopic3")
}
生成未捕获的异常:
[ScalaTest-run-running-KafkaStreamSuite] ERROR kafka.admin.TopicCommand$ - java.lang.IllegalArgumentException: Topic testTopic does not exist on ZK path 192.168.50.20:2181
at kafka.admin.TopicCommand$.deleteTopic(TopicCommand.scala:166)
at kafka.admin.TopicCommand$.main(TopicCommand.scala:68)
at myapp.kafkautilities.AdminUtilities$.deleteTopic(AdminUtilities.scala:17)
<snip>
答案 0 :(得分:1)
Kafka捕获异常,打印它然后做了令人讨厌的事情 - System.exit
。见https://github.com/apache/kafka/blob/0.10.1.1/core/src/main/scala/kafka/admin/TopicCommand.scala#L76:
...
67: else if(opts.options.has(opts.deleteOpt))
68: deleteTopic(zkUtils, opts)
69:} catch {
70: case e: Throwable =>
71: println("Error while executing topic command : " + e.getMessage)
72: error(Utils.stackTrace(e))
73: exitCode = 1
74:} finally {
75: zkUtils.close()
76: System.exit(exitCode)
77:}