Functions marked with throws can stop normal execution and report an error.

Reject invalid input

rawScore
throwing_functions.swift
Replay: real traced execution (multi-file project)
enum ScoreError: Error {
    case belowMinimum
}

func checkedScore(_ score: Int) throws -> String {
    if score < 0 {
        throw ScoreError.belowMinimum
    }
    return "score=\(score)"
}

let rawScore = 8
let message: String

do {
    message = try checkedScore(rawScore)
} catch ScoreError.belowMinimum {
    message = "score rejected"
}

print(message)
enum ScoreError: Error {
    case belowMinimum
}

func checkedScore(_ score: Int) throws -> String {
    if score < 0 {
        throw ScoreError.belowMinimum
    }
    return "score=\(score)"
}

let rawScore = -2
let message: String

do {
    message = try checkedScore(rawScore)
} catch ScoreError.belowMinimum {
    message = "score rejected"
}

print(message)
enum ScoreError: Error {
    case belowMinimum
}

func checkedScore(_ score: Int) throws -> String {
    if score < 0 {
        throw ScoreError.belowMinimum
    }
    return "score=\(score)"
}

let rawScore = 15
let message: String

do {
    message = try checkedScore(rawScore)
} catch ScoreError.belowMinimum {
    message = "score rejected"
}

print(message)
  1. rawScore ← 8

    12let rawScore→ 8 = 8  //@rawScore=-2, 1513let message: String1415do {16    message = try checkedScore(rawScore8)17} catch ScoreError.belowMinimum {
  2. func checkedScore(_ score: Int) throws -> String

    5func checkedScore(_ score8: Int) throws -> String {6    if score < 0 {7        throw ScoreError.belowMinimum8    }9    return "score=\(score8)"10}
  3. message ← score=8

    15do {16    message→ score=8 = try checkedScore(rawScore8)17} catch ScoreError.belowMinimum {18    message = "score rejected"19}2021print(messagescore=8)
    outputscore=8
  1. rawScore ← -2

    12let rawScore→ -2 = -213let message: String1415do {16    message = try checkedScore(rawScore-2)17} catch ScoreError.belowMinimum {
  2. func checkedScore(_ score: Int) throws -> String

    5func checkedScore(_ score-2: Int) throws -> String {6    if score < 0 {
  3. if score < 0

    5func checkedScore(_ score: Int) throws -> String {6    if score-2 < 0 {7        throw ScoreError.belowMinimum8    }
  4. message ← score rejected

    17} catch ScoreError.belowMinimum {18    message→ score rejected = "score rejected"19}2021print(messagescore rejected)
    outputscore rejected
  1. rawScore ← 15

    12let rawScore→ 15 = 1513let message: String1415do {16    message = try checkedScore(rawScore15)17} catch ScoreError.belowMinimum {
  2. func checkedScore(_ score: Int) throws -> String

    5func checkedScore(_ score15: Int) throws -> String {6    if score < 0 {7        throw ScoreError.belowMinimum8    }9    return "score=\(score15)"10}
  3. message ← score=15

    15do {16    message→ score=15 = try checkedScore(rawScore15)17} catch ScoreError.belowMinimum {18    message = "score rejected"19}2021print(messagescore=15)
    outputscore=15
throwing function A throwing function uses `throw` to leave the normal path when it cannot produce a valid result.