bhylu.com

Advertisements

Swift Error Handling

Return a structured failure when a duration cannot satisfy its contract.

How it works

A throwing function marks recoverable failure with throws. A caller uses try and handles errors with do-catch or propagates them. Prefer explicit error cases when callers need to distinguish syntax, range and other failures. A trap or a forced try is not a normal rejected-input policy.

Run this Swift example

Install the toolchain for your platform from Swift.org, then check swift --version. The reviewed portable baseline is Swift 6.4.2 in Swift 6 mode on Ubuntu 26.04 x86_64 through the PC’s existing WSL installation. These are real Linux executions, not claims of Windows-native or Apple-platform test results. Windows Swift has separate C++/SDK prerequisites in its official setup guide.

Save the complete source as error-handling.swift in a new learning folder. On Linux/macOS compile with swiftc -swift-version 6 -parse-as-library error-handling.swift -o lesson, then run ./lesson. On a configured Windows toolchain use an output such as lesson.exe and run .\lesson.exe. The parse-as-library option supports the explicit @main entry point used by these sources. Compile each ordinary lesson separately.

Bhylu has no Swift browser runner. The JavaScript/Python editors do not run these sources; Apache, PHP and MySQL are not required. Downloads are source text, never executable teaching endpoints.

Download the complete error-handling.swift · Download all source files with hash checks

For the optional collection, save download-examples.mjs in a new folder and run node download-examples.mjs. A local preview uses node download-examples.mjs http://127.0.0.1:8000/examples/swift/. It refuses an existing bhylu-swift-examples folder and installs or executes no Swift automatically. Apple-only files remain separate Xcode learning examples, not a portable Swift package target.

Example

error-handling.swift

enum DurationError: Error { case syntax, range }
func parseMinutes(_ text: String) throws -> Int {
    guard let value = Int(text) else { throw DurationError.syntax }
    guard (1...240).contains(value) else { throw DurationError.range }
    return value
}

@main
struct Lesson {
    static func main() {
        for raw in ["30", "0", "bad"] {
            do { print(try parseMinutes(raw)) }
            catch DurationError.syntax { print("invalid syntax") }
            catch DurationError.range { print("out of range") }
            catch { print("unexpected failure") }
        }
    }
}

Expected result

30
out of range
invalid syntax

The helper first checks whole-number parsing, then the one-to-two-hundred-forty-minute domain. The caller handles known cases without comparing message strings. Unanticipated errors remain distinct rather than being reported as successful zero minutes.

Primary references: The Swift Programming Language and Apple framework documentation. Use the selected platform’s current requirements. Fictional workshop examples do not establish production safety or deployment approval.

Advertisements