简体   繁体   中英

Scala Anorm Query Exception Handling

I would like to understand how to best handle exceptions with Anorm using Scala's Try and match features

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.

In your example, you call the .execute() method which returns a Boolean. ie true for Success and false for a Failure.

If you want a more detailed result in case of executions, you could use .executeQuery() instead

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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