简体   繁体   English

Scala Anorm查询异常处理

[英]Scala Anorm Query Exception Handling

I would like to understand how to best handle exceptions with Anorm using Scala's Try and match features 我想了解如何使用Scala的“尝试并匹配”功能最好地处理Anorm异常

Given a query 给定一个查询

database.withConnection { implicit c =>
  SQL("Select 1").execute()
}

How would I catch exceptions ? 我将如何捕获异常? I imagine something like 我想像

match {
   case Success(?) => ?
   case Failure(ex) => println(ex) ? 
}

Any working example would be much appreciated. 任何可行的例子将不胜感激。 I'd like to be able to handle any unexpected database related exceptions gracefully. 我希望能够优雅地处理任何与数据库有关的意外异常。

The withConnection , withTransaction etc. methods' return values depends on the code block you're running. withConnectionwithTransaction等方法返回值取决于你正在运行的代码块。

In your example, you call the .execute() method which returns a Boolean. 在您的示例中,您调用了.execute()方法,该方法返回一个布尔值。 ie true for Success and false for a Failure. 即,对于成功为true ,对于失败为false

If you want a more detailed result in case of executions, you could use .executeQuery() instead 如果您希望在执行时获得更详细的结果,则可以使用.executeQuery()代替

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM