Ian Warburton Ian Warburton - 1 month ago 11
Swift Question

Avoid handling all exceptions until they've reached a global handler

If I call

hello.f
, the compiler forces me to handle all the thrown exceptions. I can swallow them using
try?
or add a
catch
all by not specifying the type of exception to catch and then re-throw. But, if I do the latter, I need to mark the calling function as throws and the process continues all the way up the stack.

Is it possible to ignore
f
's exceptions such that they ultimately become un-handled exceptions that can be handled globally?

enum e : Error {
case hello
case world
}

class hello {
func f() throws {
throw e.hello
}
}

Answer

Is it possible to ignore f's exceptions such that they ultimately become un-handled exceptions that can be handled globally?

In a word, no. The entire point of the design of Swift's error handling is that you must explicitly acknowledge the possibility of error at every stage.

It sounds to me like you're thinking of how Objective-C deals with NSException, but Swift isn't like that (and Error is not like NSException).

Comments