Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix catch_pattern_list rule of swift5 parser #4382

Merged
merged 6 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion swift/swift5/Swift5Parser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ catch_clause
;

catch_pattern_list
: catch_pattern (catch_pattern COMMA catch_pattern)*
: catch_pattern (COMMA catch_pattern)*
;

catch_pattern
Expand Down
24 changes: 24 additions & 0 deletions swift/swift5/examples/Error Handling/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ enum PrinterError: Error {
case onFire
}

// One more error
enum JustError: Error {
case someError
}

//: Use `throw` to throw an error and `throws` to mark a function that can throw an error. If you throw an error in a function, the function returns immediately and the code that called the function handles the error.
//:
func send(job: Int, toPrinter printerName: String) throws -> String {
if printerName == "Never Has Toner" {
throw PrinterError.noToner
}
if printerName == "Throw error" {
throw JustError.someError
}
return "Job sent"
}

Expand Down Expand Up @@ -42,6 +50,22 @@ do {
print(error)
}

//: - Experiment:
//: Change the printer name to `"Throw error"`, so that the `send(job:toPrinter:)` function throws an error.
//:
//: You can catch multiple error cases inside a single catch block
//:
do {
let printerResponse = try send(job: 1440, toPrinter: "Gutenberg")
print(printerResponse)
} catch is JustError, PrinterError.noToner {
print("Error.")
} catch let printerError as PrinterError {
print("Printer error: \(printerError).")
} catch {
print(error)
}

//: - Experiment:
//: Add code to throw an error inside the `do` block. What kind of error do you need to throw so that the error is handled by the first `catch` block? What about the second and third blocks?
//:
Expand Down
Loading