I'm trying to create a DB Transaction following a safe path. So I'd like to do something using "do catch" statement, I'm wondering something like that:
do {
try? db.endTransaction()
}catch{ //THIS WHOLE CODE CRASHES
print(error.localizedDescription)
db.rollbackTransaction()
}
However, something is going wrong using this syntaxe. What am I doing wrong? Is this the best way to do what I want?
答案 0 :(得分:0)
Inside a do
block, you should remove the ?
on try
because the whole point of do
is to catch errors that are thrown by things called with try
. Once you do that, if your endTransaction()
throws an exception, it should be caught by your catch
block.