Swift Throw Statement

In this tutorial you will learn about the Swift Throw Statement and its application with practical example.

Swift Throw Statement

In swift, the throw statement is an error handling statement. The throw statement allows function to stop the execution and throw an error if there is any exception or error occurs. In Swift, if a function, method, or an initializer can throw an error, it must be added throws keyword in its definition after the parameter list and before the return type.

Table Of Contents

If a function or an initializer can throw an error, the throws modifier must be added in the definition itself right after the paratheses and just before the return type (->).

Syntax:-

Example:-

When we run the above swift program, we see the following output –

Output:-

swift_throw_statement

swift_throw_statement

 

In this tutorial we have learn about the Swift Throw Statement and its application with practical example. I hope you will like this tutorial.