Guard statements should allow you to catch errors via a catch block

Countless times I have to write try statements and most circumstances require me have a guard statement in place so I dont have a optional object. is there a way we can add catch blocks in guard statements to do better handling. I say this because I h8 "do-catch" blocks . Something like below is what I am thinking. Instead of "guard-else", you can add "guard-catch". The example below shows a readDisk func that has "throws" and returns an optional.

 // func readDisk() throws -> DiskObject?
 guard let response = try? readDisk(from: "someDirectory") catch {
     let _ = ShowAlert()
     return 
 }

This has come up many times before. It would probably be better to pick up one of those threads instead of making a new one.

3 Likes

Understood. Thanks for the Swift reply

2 Likes

*ba-dum tsh*

8 Likes