Search code examples
swiftintdoubletry-catchfatal-error

Catching an error from a non-throwing method


Swift

Integers in Swift are ok. They cannot be infinite like Doubles, thought. They have a limit.
Surpassing that limit will cause a crash.


Exhibit A
Int(10000000000000000000)
error: integer literal '10000000000000000000' overflows when stored into 'Int'


Exhibit B
Int(pow(Double(1000000000), Double(10)))
Fatal error: Double value cannot be converted to Int because the result would be greater than Int.max


I naively thought to myself, "Hey, this is a fatal error. Can I catch the error with a do, catch block?"
Nope


Exhibit C

do {
    Int(pow(Double(1000000000), Double(10)))
} catch {
    print("safety net")
}
print("good?")

warning: 'catch' block is unreachable because no errors are thrown in 'do' block
Fatal error: Double value cannot be converted to Int because the result would be greater than Int.max


Oh, yeah. That's right! I forgot to add try
Nope


Exhibit D

do {
    try Int(pow(Double(1000000000), Double(10)))
} catch {
    print("safety net")
}
print("good?")

warning: no calls to throwing functions occur within 'try' expression
warning: 'catch' block is unreachable because no errors are thrown in 'do' block
Fatal error: Double value cannot be converted to Int because the result would be greater than Int.max


What is going on?

Can anyone explain this to me? I would really like to be able to catch an error like this.
Thank you so much, this would be a huge help!


Solution

  • 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
    }