This commit is contained in:
commit
568474d52f
1591 files changed
+528213
No files matched your search
+101
@@ -0,0 +1,101 @@
|
||||
# These are equivalent
|
||||
raise Error("file not found")
|
||||
raise "file not found"
|
||||
```
|
||||
|
||||
The string literal form is a convenience—the compiler automatically wraps it in
|
||||
an `Error`.
|
||||
|
||||
By declaring `raises`, you tell Mojo that a function may raise an error:
|
||||
|
||||
```mojo
|
||||
def read_file_fn(path: String) raises -> String:
|
||||
if not path:
|
||||
raise "path cannot be empty"
|
||||
return "contents of " + path
|
||||
```
|
||||
|
||||
:::tip
|
||||
|
||||
If you need structured error data—like separate fields for an error code and
|
||||
description—or allocation-free errors for GPU kernels, see
|
||||
[Typed errors](#typed-errors) later on this page. For most application code,
|
||||
`Error` with a descriptive string message is all you need.
|
||||
|
||||
:::
|
||||
|
||||
## Handle an error
|
||||
|
||||
Mojo uses `try`/`except` to detect and handle errors. The full syntax is:
|
||||
|
||||
```mojo
|
||||
try:
|
||||
# Code that might raise an error
|
||||
except e:
|
||||
# Runs if an error occurs
|
||||
else:
|
||||
# Runs if no error occurs
|
||||
finally:
|
||||
# Always runs, regardless of outcome
|
||||
```
|
||||
|
||||
You must include one or both of `except` and `finally`. The `else` clause is
|
||||
optional.
|
||||
|
||||
### How each clause works
|
||||
|
||||
- `try` — Contains code that might raise an error. If no error occurs, the
|
||||
entire block executes. If an error occurs, execution stops at the `raise`
|
||||
point and continues with the `except` clause (if present) or the `finally`
|
||||
clause.
|
||||
|
||||
- `except` — Runs only when an error occurs in the `try` block. If you
|
||||
provide a variable name (`except e:`), the error is bound to that variable.
|
||||
A `try` block can have only one `except` clause.
|
||||
|
||||
- `else` — Runs only when no error occurs in the `try` block. The `else`
|
||||
clause is *skipped* if the `try` clause exits via `continue`, `break`, or
|
||||
`return`.
|
||||
|
||||
- `finally` — Runs after the `try` and any `except` or `else` clause,
|
||||
regardless of outcome. It executes even if another clause exits via
|
||||
`continue`, `break`, `return`, or by raising a new error. Use `finally` to
|
||||
release resources (such as file handles) that must be cleaned up regardless
|
||||
of whether an error occurred.
|
||||
|
||||
### Example
|
||||
|
||||
The following example demonstrates all four clauses. The `process_record()`
|
||||
function raises `Error` for different conditions, and the caller loops over
|
||||
a list of IDs to exercise each clause:
|
||||
|
||||
```mojo title="handle_error.mojo"
|
||||
def process_record(id: Int) raises -> String:
|
||||
if id < 0:
|
||||
raise Error("invalid record ID: must be non-negative")
|
||||
if id > 999:
|
||||
raise Error("record not found")
|
||||
return String("record_", id)
|
||||
|
||||
|
||||
def main() raises:
|
||||
try:
|
||||
for id in [5, 0, 1001, -3, 42]:
|
||||
var result: String
|
||||
try:
|
||||
print()
|
||||
print("try => id:", id)
|
||||
if id == 0:
|
||||
continue
|
||||
result = process_record(id)
|
||||
except e:
|
||||
if "invalid" in String(e):
|
||||
print("except => fatal:", e)
|
||||
raise e
|
||||
print("except => handled:", e)
|
||||
else:
|
||||
print("else => success:", result)
|
||||
finally:
|
||||
print("finally => done with id:", id)
|
||||
except e:
|
||||
print("\nre-raised error:", e)
|
||||
Reference in new issue
Block a user