-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path05_error_handling.mojo
More file actions
75 lines (57 loc) · 2.15 KB
/
05_error_handling.mojo
File metadata and controls
75 lines (57 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Example 05: Error Handling
#
# Demonstrates: Handling JSON parse errors using try/except and assert_raises
from std.testing import assert_raises
from json import loads, dumps, Value
def parse_safely(json_str: String) -> String:
"""Attempt to parse JSON and return result or error message."""
try:
var result = loads(json_str)
return "OK: " + dumps(result)
except e:
return "Error: " + String(e)
def main() raises:
print("JSON Error Handling Examples")
print("=" * 40)
print()
# Valid JSON examples
print("Valid JSON:")
print(" '42' ->", parse_safely("42"))
print(" 'true' ->", parse_safely("true"))
print(" '\"hello\"' ->", parse_safely('"hello"'))
print(" '[1,2,3]' ->", parse_safely("[1,2,3]"))
print(" '{\"a\":1}' ->", parse_safely('{"a":1}'))
print()
# Invalid JSON - using assert_raises to verify errors are raised
print("Invalid JSON (verified with assert_raises):")
with assert_raises():
_ = loads("") # empty
print(" '' (empty) -> correctly raises error")
with assert_raises():
_ = loads("{") # unclosed
print(" '{' (unclosed) -> correctly raises error")
with assert_raises():
_ = loads("[1,2,") # incomplete
print(" '[1,2,' (incomplete) -> correctly raises error")
with assert_raises():
_ = loads('{"key":}') # missing value
print(" '{\"key\":}' (missing value) -> correctly raises error")
with assert_raises():
_ = loads("undefined") # not JSON
print(" 'undefined' (not JSON) -> correctly raises error")
with assert_raises():
_ = loads("{key: 1}") # unquoted key
print(" '{key: 1}' (unquoted key) -> correctly raises error")
print()
# Practical error handling pattern
print("Practical usage pattern:")
var user_input = '{"name": "Alice", "age": 30}'
try:
var data = loads(user_input)
print(" Successfully parsed user data")
print(" Data:", dumps(data))
except e:
print(" Failed to parse user data:", e)
print(" Using default values instead...")
print()
print("All error handling tests passed!")