我想在Swift中做一些我习惯在其他多种语言中做的事情:用自定义消息抛出运行时异常。例如(在Java中):
throw new RuntimeException("A custom message here")
我知道我可以抛出符合ErrorType协议的枚举类型,但我不希望必须为抛出的每种类型的错误定义枚举。理想情况下,我希望能够尽可能地模拟上面的示例。我考虑创建一个实现ErrorType协议的自定义类,但我甚至不知道该协议需要什么。想法吗?
我想在Swift中做一些我习惯在其他多种语言中做的事情:用自定义消息抛出运行时异常。例如(在Java中):
throw new RuntimeException("A custom message here")
我知道我可以抛出符合ErrorType协议的枚举类型,但我不希望必须为抛出的每种类型的错误定义枚举。理想情况下,我希望能够尽可能地模拟上面的示例。我考虑创建一个实现ErrorType协议的自定义类,但我甚至不知道该协议需要什么。想法吗?
当前回答
最简单的解决方案,没有额外的扩展,枚举,类等:
NSException(name:NSExceptionName(rawValue: "name"), reason:"reason", userInfo:nil).raise()
其他回答
最简单的方法是使String符合Error:
extension String: Error {}
然后你可以抛出一个字符串:
throw "Some Error"
为了让字符串本身成为错误的localizedString,你可以扩展LocalizedError:
extension String: LocalizedError {
public var errorDescription: String? { return self }
}
最简单的解决方案,没有额外的扩展,枚举,类等:
NSException(name:NSExceptionName(rawValue: "name"), reason:"reason", userInfo:nil).raise()
我想对所提议的一些解决办法提出一些变通建议:
public enum MyError: Error {
var localizedDescription: String {
get {
switch(self) {
case .network(let message, let code):
return "\(message) (\(code))"
case .invalidInput(message: let message):
return message
}
}
}
case network(message: String, code: Int)
case invalidInput(message: String)
}
它需要更多的工作来创建,但它提供了所有世界中最好的:
它是一个枚举,所以可以在switch语句中使用。 所有错误都必须创建一个消息,即使对于相同类型的错误也可以是不同的消息(与扩展String的枚举不同) 它在localizedDescription下提供了每个开发人员都期望的消息。
根据@Nick keets的回答,这里有一个更完整的例子:
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
}
最初发表于我的swift博客:http://eon.codes/blog/2017/09/01/throwing-simple-errors/
抛出代码应该清楚错误消息是适合显示给最终用户还是仅用于开发人员调试。为了表明描述可以显示给用户,我使用了实现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