Day 16: Exceptions - String to Integer

  • + 0 comments

    import Foundation

    /* * Define an ErrorType */ enum StringToIntTypecastingError: Error { case BadString }

    func stringToInt(inputString: String) throws -> Int {
        guard let int = Int(inputString) else {
            throw StringToIntTypecastingError.BadString
        }
        return int
    }
    

    /* * Read the input */ let inputString = readLine()!

    do { try print(stringToInt(inputString: inputString)) } catch StringToIntTypecastingError.BadString { print("Bad String") }

    **// I am getting Error reading result file.You should use exception handling concepts.

    but output printing correct can any one please help in this in Swift language **