Jay as a memojiJay Wilson

How to Catch Specific Error Types in Swift

A quick snippet on how to catch specific error types

Published:
Originally posted here

I always forget how to do this and luckily Natalie Panferova at Nilcoalescing wrote a great article about how to provide user friendly descriptions and how to catch specific error types.

I’m mainly writing this so I have access to the part of that article that I forget, which is how to catch a specific error type.

do {
    // Some type of work here
} catch let error as ErrorTypeYouWantToCatch {
    // Do something with that error
} catch {
    // Don't forget to have a catchall incase the
    // error isn't what you expected
}

Some benefits to catching error types is to present proper user facing notifications or to make the logger calls more useful to you as the developer.

More resources


Written by Jay Wilson on


Thanks for reading! If you found this post useful consider buying me a coffee, so I can keep making posts like this.

No comments yet. Share on mastodon and see your comment or write a post on your blog if you support Webmentions

These people reposted this post on various platforms around the internet.

These people liked this post on various platforms around the internet.

These people bookmarked this post on various platforms around the internet.

Powered by Webmentions