How I can wrap in do
catch
overflow error
let increasePerSecond: UInt32 = UInt32.max
let offset: UInt32
do {
offset = try ((nowTimeInterval - calculatedTimeInterval) * increasePerInterval)
} catch _ {
offset = 0
}
But if I have error in do
section I do not get in catch
and have crash my app
UPD: It's not about how to get var offset, the question is how to handle the error ?
Overflow in integer arithmetic does not throw
an error, therefore you cannot catch
it. If you
want to detect and handle overflow, you can use one of the ...WithOverflow
methods of the integer types. Example:
let a = UInt32.max
let b = UInt32(2)
if case let (result, overflow) = UInt32.multiplyWithOverflow(a, b), !overflow {
print(result)
} else {
print("overflow")
}