问题描述:

If I want to make a function in Swift2 that will throw an error. But I do not want to use try! all the time. I use the code like this but It doesn't work.

enum MyError: ErrorType {

case aError

}

func afun() throws -> AnyObject? {

guard 1>2 else {

throw MyError.aError

}

return nil

}

func bfun() {

try! afun()

}

do {

try bfun()

}

catch {

print("error appear")

}

It will show like this

Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0).

But I hope I can print the error log and run the next other code.

or If I want to write code like this:

do {

try 3/0

} catch {

print("error")

}

I don't know the Error Type but I cannot use try catch to avoid this problem.

相关阅读:
Top