Simplest way to throw an error/exception with a custom message in Swift 2?

前端 未结 10 1851
走了就别回头了
走了就别回头了 2020-12-22 15:57

I want to do something in Swift 2 that I\'m used to doing in multiple other languages: throw a runtime exception with a custom message. For example (in Java):



        
相关标签:
10条回答
  • 2020-12-22 16:35

    The simplest approach is probably to define one custom enum with just one case that has a String attached to it:

    enum MyError: ErrorType {
        case runtimeError(String)
    }
    

    Or, as of Swift 4:

    enum MyError: Error {
        case runtimeError(String)
    }
    

    Example usage would be something like:

    func someFunction() throws {
        throw MyError.runtimeError("some message")
    }
    do {
        try someFunction()
    } catch MyError.runtimeError(let errorMessage) {
        print(errorMessage)
    }
    

    If you wish to use existing Error types, the most general one would be an NSError, and you could make a factory method to create and throw one with a custom message.

    0 讨论(0)
  • 2020-12-22 16:36

    I like @Alexander-Borisenko's answer, but the localized description was not returned when caught as an Error. It seems that you need to use LocalizedError instead:

    struct RuntimeError: LocalizedError
    {
        let message: String
    
        init(_ message: String)
        {
            self.message = message
        }
    
        public var errorDescription: String?
        {
            return message
        }
    }
    

    See this answer for more details.

    0 讨论(0)
  • 2020-12-22 16:39

    Throwing code should make clear whether the error message is appropriate for display to end users or is only intended for developer debugging. To indicate a description is displayable to the user, I use a struct DisplayableError that implements the LocalizedError protocol.

    struct DisplayableError: Error, LocalizedError {
        let errorDescription: String?
    
        init(_ description: String) {
            errorDescription = description
        }
    }
    

    Usage for throwing:

    throw DisplayableError("Out of pixie dust.")
    

    Usage for display:

    let messageToDisplay = error.localizedDescription
    
    0 讨论(0)
  • 2020-12-22 16:46

    Based on @Nick keets answer, here is a more complete example:

    extension String: Error {} // Enables you to throw a string
    
    extension String: LocalizedError { // Adds error.localizedDescription to Error instances
        public var errorDescription: String? { return self }
    }
    
    func test(color: NSColor) throws{
        if color == .red {
            throw "I don't like red"
        }else if color == .green {
            throw "I'm not into green"
        }else {
            throw "I like all other colors"
        }
    }
    
    do {
        try test(color: .green)
    } catch let error where error.localizedDescription == "I don't like red"{
        Swift.print ("Error: \(error)") // "I don't like red"
    }catch let error {
        Swift.print ("Other cases: Error: \(error.localizedDescription)") // I like all other colors
    }
    

    Originally published on my swift blog: http://eon.codes/blog/2017/09/01/throwing-simple-errors/

    0 讨论(0)
  • 2020-12-22 16:49

    Swift 4:

    As per:

    https://developer.apple.com/documentation/foundation/nserror

    if you don't want to define a custom exception, you could use a standard NSError object as follows:

    import Foundation
    
    do {
      throw NSError(domain: "my error domain", code: 42, userInfo: ["ui1":12, "ui2":"val2"] ) 
    }
    catch let error as NSError {
      print("Caught NSError: \(error.localizedDescription), \(error.domain), \(error.code)")
      let uis = error.userInfo 
      print("\tUser info:")
      for (key,value) in uis {
        print("\t\tkey=\(key), value=\(value)")
      }
    }
    

    Prints:

    Caught NSError: The operation could not be completed, my error domain, 42
        User info:
            key=ui1, value=12
            key=ui2, value=val2
    

    This allows you to provide a custom string (the error domain), plus a numeric code and a dictionary with all the additional data you need, of any type.

    N.B.: this was tested on OS=Linux (Ubuntu 16.04 LTS).

    0 讨论(0)
  • 2020-12-22 16:50

    In case you don't need to catch the error and you want to immediately stop the application you can use a fatalError: fatalError ("Custom message here")

    0 讨论(0)
提交回复
热议问题