Catching throw from initializer

I have a struct that can throw an error.

If I just wanted to know if it had succeeded or not, I would use
Code Block swift
guard let try? thing = Thing()
else { terminate }

But I want to catch the error so that I can give a useful message before terminating, and I don't think I can do/catch in a guard statement.. The best I can come up with is

Code Block swift
let thing: Thing
do
try thing = Thing()
catch ... {
give useful message
terminate
}

That isn't as attractive in several ways. Is there a better way?
I meant to say that I have a struct whose initializer can throw an error.

That isn't as attractive in several ways. Is there a better way?

What do you think attractive? What do you think better? What is the reason you think it is not attractive?

What do you think attractive? What do you think better? What is the reason you think it is not attractive?

(1) Having to separate the declaration and the initialization of a let constant is sometimes necessary, but always feels like a hack. One of the big advantages of guard is that I can initialize a let constant with an optional binding, without having to wrap the entire body of a function in an if statement.

(2) A guard statement says immediately, “if his doesn't work, I’m out of here.” The do/catch requires a second look to determine that it’s really moral equivalent of a guard.



 always feels like a hack. 

So, the main reason is you feel it like a hack.

As far as I know, there are no ways other than using do-catch.
But you may want to wait for someone who knows a real hack for this issue.
Or you can suggest an evolution on Swift forum.
A hacky workaround for people who thinks using guard is really a big issue.

Prepare some extension for Swift.Result.
Code Block
extension Result {
func getError() -> Error? {
switch self {
case .failure(let error):
return error
default:
return nil
}
}
}


And then, you can write something like this:
Code Block
let result = Result(catching: {try Thing()})
guard case .success(let thing) = result else {
print(result.getError()!)
exit(1)
}


I do not think it is any better than do-catch, but it actually is using guard.
Catching throw from initializer
 
 
Q