Syntax

Swift Do / Try / Catch Syntax

In Swift, do / try / catch: handles thrown errors — do { try run() } catch { print(error) }

Quick Answer

Syntax
do { try run() } catch { print(error) }
SwiftSyntax

Do / Try / Catch

Code example
do { try run() } catch { print(error) }

Handles thrown errors.

Learn More

How to do / try / catch in Swift

  1. Open your Swift file or editor.
  2. Copy the example, then replace its sample names and values for your code.

Swift Do / Try / Catch Syntax

Use do { try run() } catch { print(error) }.

Related Syntax References

Frequently asked questions

What is the Swift syntax for do / try / catch?

Use this pattern: do { try run() } catch { print(error) }

Can I copy this Swift code example?

Yes. Use Copy code and adapt the example to your program.

Is this Swift syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify