How Does One Trap Arithmetic Overflow Errors in Swift

How does one trap arithmetic overflow errors in Swift?

Distinguish between an exception and a runtime error. An exception is thrown and can be caught. A runtime error stops your program dead in its tracks. Adding and getting an overflow is a runtime error, plain and simple. There is nothing to catch.

The point of an operator like &+ is that it doesn't error and it doesn't tell you there was a problem. That is the whole point.

If you think you might overflow, and you want to know whether you did, use static methods like addWithOverflow. It returns a tuple consisting of the result and a Bool stating whether there was an overflow.

var x: Int8 = 100
let result = x &+ x // -56

x = 100
let result2 = Int8.addWithOverflow(x,x) // (-56, true)

Do-Catch and overflow

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")
}

When Int overflow cause IOS app crash, how can I remark it

If the result of an integer arithmetic operation (+, -, *, /, ...)
overflows, the application terminates immediately. There is no way to
catch this situation or to get notified e.g. to save data.
There is no Swift error or NSException thrown which you could catch.
The same would happen for other runtime errors like accessing
an array element outside of the valid bounds, or unwrapping an
optional which is nil.

This means that you have to check beforehand if the integer arithmetic
operation can be executed. Alternatively – depending on your needs –
you can

  • use the "Overflow operators" &+, &- and &* instead,
    which truncate the result instead of triggering an error,
    similar as in (Objective-)C.
  • use addingReportingOverflow() and similar methods which “return the sum of this value and the given value, along with a Boolean value indicating whether overflow occurred in the operation.”

Swift: How to disable integer overflow / underflow traps for a function

You can use addWithOverflow or subtractWithOverflow class method of Int, UInt8 etc types

E.g. let b = UInt8.subtractWithOverflow(a, 237)

Handling Arithmetic Overflow UInt32 (CUnsignedInt)

You can use these method here, I believe it's also the same methods across all Int of different sizes

UInt32.addWithOverflow(x,y)
UInt32.multiplyWithOverflow(x, y)
UInt32.divideWithOverflow(x, y)
UInt32.subtractWithOverflow(x, y)

each of them returns a tuple of size 2 with the result and whether it overflowed or not.

Referenced from How does one trap arithmetic overflow errors in Swift?

Catching an error from a non-throwing method

You can use the init(exactly:) constructor, it will not throw an error but it will return nil if the value is to large

guard let value = Int(exactly: pow(Double(1000000000), Double(10))) else {
//error handling
}

Swift Error: Type Underflow: Exec_Bad_Instruction

It's not an error, it's a feature of Swift. Swift will intentionally crash whenever you create an arithmetic overflow or underflow.

You will get an arithmetic underflow whenever arc4random_uniform(181) returns a value less than 90 and you try to subtract 90 from it. Since the result is not in the range of an unsigned 32 bit integer, Swift informs you about your mistake by crashing your program.

And it's not a "Double" error: You never get that far that the type "Double" is involved in this at all. It will crash just the same without the cast to Double.



Related Topics



Leave a reply



Submit