我想在Swift中做一些我习惯在其他多种语言中做的事情:用自定义消息抛出运行时异常。例如(在Java中):

throw new RuntimeException("A custom message here")

我知道我可以抛出符合ErrorType协议的枚举类型,但我不希望必须为抛出的每种类型的错误定义枚举。理想情况下,我希望能够尽可能地模拟上面的示例。我考虑创建一个实现ErrorType协议的自定义类,但我甚至不知道该协议需要什么。想法吗?


当前回答

抛出代码应该清楚错误消息是适合显示给最终用户还是仅用于开发人员调试。为了表明描述可以显示给用户,我使用了实现LocalizedError协议的struct DisplayableError。

struct DisplayableError: Error, LocalizedError {
    let errorDescription: String?

    init(_ description: String) {
        errorDescription = description
    }
}

投掷用途:

throw DisplayableError("Out of pixie dust.")

显示用途:

let messageToDisplay = error.localizedDescription

其他回答

我喜欢@Alexander-Borisenko的回答,但是当被捕捉为错误时,本地化的描述没有返回。看起来你需要使用LocalizedError代替:

struct RuntimeError: LocalizedError
{
    let message: String

    init(_ message: String)
    {
        self.message = message
    }

    public var errorDescription: String?
    {
        return message
    }
}

更多细节请看这个答案。

最简单的方法可能是定义一个自定义enum,只有一个case,并附加一个String:

enum MyError: ErrorType {
    case runtimeError(String)
}

或者,在Swift 4中:

enum MyError: Error {
    case runtimeError(String)
}

示例用法如下:

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

如果您希望使用现有的错误类型,最常用的错误类型是NSError,您可以创建一个工厂方法来创建并抛出一个带有自定义消息的错误类型。

看看这个很酷的版本。其思想是同时实现String和ErrorType协议,并使用错误的rawValue。

enum UserValidationError: String, Error {
  case noFirstNameProvided = "Please insert your first name."
  case noLastNameProvided = "Please insert your last name."
  case noAgeProvided = "Please insert your age."
  case noEmailProvided = "Please insert your email."
}

用法:

do {
  try User.define(firstName,
                  lastName: lastName,
                  age: age,
                  email: email,
                  gender: gender,
                  location: location,
                  phone: phone)
}
catch let error as User.UserValidationError {
  print(error.rawValue)
  return
}

首先,让我们看一些使用示例,然后如何使这些示例工作(定义)。

使用

do {
    throw MyError.Failure
} catch {
    print(error.localizedDescription)
}

或更具体的风格:

do {
    try somethingThatThrows()
} catch MyError.Failure {
    // Handle special case here.
} catch MyError.Rejected {
    // Another special case...
} catch {
    print(error.localizedDescription)
}

此外,分类也是可能的:

do {
    // ...
} catch is MyOtherErrorEnum {
    // If you handle entire category equally.
} catch let error as MyError {
    // Or handle few cases equally (without string-compare).
    switch error {
    case .Failure:
        fallthrough;
    case .Rejected:
        myShowErrorDialog(error);
    default:
        break
    }
}

定义

public enum MyError: String, LocalizedError {
    case Failure = "Connection fail - double check internet access."
    case Rejected = "Invalid credentials, try again."
    case Unknown = "Unexpected REST-API error."

    public var errorDescription: String? { self.rawValue }
}

利与弊

Swift自动定义错误变量,处理程序只需要读取localizedDescription属性。

但这是模糊的,我们应该使用“catch MyError”。Failure{}”的样式代替(以明确我们处理的情况),尽管,分类是可能的,如使用示例所示。

Teodor-Ciuraru's answer (which's almost equal) still needs a long manual cast (like "catch let error as User.UserValidationError { ... }"). The accepted categorization-enum approach's disadvantages: Is too vague as he comments himself, so that catchers may need to compare String message!? (just to know exact error). For throwing same more than once, needs copy/pasting message!! Also, needs a long phrase as well, like "catch MyError.runtimeError(let errorMessage) { ... }". The NSException approach has same disadvantages of categorization-enum approach (except maybe shorter catching paragraph), also, even if put in a factory method to create and throw, is quite complicated.

结论

通过简单地使用LocalizedError而不是Error,这完成了其他现有的解决方案,并希望将像我这样的人从阅读所有其他帖子中拯救出来。

(我的懒惰有时会给我带来很多工作。)

测试

import Foundation
import XCTest
@testable import MyApp

class MyErrorTest: XCTestCase {
    func testErrorDescription_beSameAfterThrow() {
        let obj = MyError.Rejected;
        let msg = "Invalid credentials, try again."
        XCTAssertEqual(obj.rawValue, msg);
        XCTAssertEqual(obj.localizedDescription, msg);
        do {
            throw obj;
        } catch {
            XCTAssertEqual(error.localizedDescription, msg);
        }
    }

    func testThrow_triggersCorrectCatch() {
        // Specific.
        var caught = "None"
        do {
            throw MyError.Rejected;
        } catch MyError.Failure {
            caught = "Failure"
        } catch MyError.Rejected {
            caught = "Successful reject"
        } catch {
            caught = "Default"
        }
        XCTAssertEqual(caught, "Successful reject");
    }
}

其他工具:

如果为每个枚举实现errorDescription很痛苦,那么就一次性实现它,比如:

extension RawRepresentable where RawValue == String, Self: LocalizedError {
    public var errorDescription: String? {
        return self.rawValue;
    }
}

上面只是为已经扩展了LocalizedError的枚举添加了逻辑(但是可以删除“Self: LocalizedError”部分,使其应用于任何string-enum)。

#2如果我们需要额外的上下文,如FileNotFound与文件路径相关联?请看我的另一篇文章:

https://stackoverflow.com/a/70448052/8740349

基本上,将LocalizedErrorEnum从上面的链接复制并添加到您的项目中,并根据需要使用关联枚举多次重用。

抛出代码应该清楚错误消息是适合显示给最终用户还是仅用于开发人员调试。为了表明描述可以显示给用户,我使用了实现LocalizedError协议的struct DisplayableError。

struct DisplayableError: Error, LocalizedError {
    let errorDescription: String?

    init(_ description: String) {
        errorDescription = description
    }
}

投掷用途:

throw DisplayableError("Out of pixie dust.")

显示用途:

let messageToDisplay = error.localizedDescription