-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_handling_enhanced.ruff
More file actions
357 lines (295 loc) · 8.26 KB
/
Copy patherror_handling_enhanced.ruff
File metadata and controls
357 lines (295 loc) · 8.26 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# Enhanced Error Handling Features in Ruff
#
# This example demonstrates all new error handling capabilities:
# - Error properties: err.message, err.stack, err.line
# - Custom error types with structs
# - Error chaining with cause property
# - Stack traces from nested function calls
print("==================================================")
print("Enhanced Error Handling Examples")
print("==================================================")
print("")
# ==============================================================================
# Example 1: Accessing Error Properties
# ==============================================================================
print("--- Example 1: Error Properties ---")
try {
throw("This is an error message")
} except err {
print("Error message: " + err.message)
print("Line number: " + err.line)
print("Stack depth: " + len(err.stack))
}
print("")
# ==============================================================================
# Example 2: Custom Error Types with Structs
# ==============================================================================
print("--- Example 2: Custom Error Types ---")
struct ValidationError {
field: string,
message: string,
value: string
}
func validate_email(email: string) {
if !contains(email, "@") {
error := ValidationError {
field: "email",
message: "Email must contain @ symbol",
value: email
}
throw(error)
}
return true
}
try {
validate_email("invalid-email")
} except err {
print("Validation failed: " + err.message)
}
try {
validate_email("user@example.com")
print("Email validation passed!")
} except err {
print("This won't print")
}
print("")
# ==============================================================================
# Example 3: Stack Traces in Function Calls
# ==============================================================================
print("--- Example 3: Stack Traces ---")
func function_a() {
function_b()
}
func function_b() {
function_c()
}
func function_c() {
throw("Error from function_c")
}
try {
function_a()
} except err {
print("Caught error: " + err.message)
print("Stack trace:")
i := 0
while i < len(err.stack) {
frame := err.stack[i]
print(" [" + i + "] " + frame)
i := i + 1
}
}
print("")
# ==============================================================================
# Example 4: Error Chaining
# ==============================================================================
print("--- Example 4: Error Chaining ---")
struct DatabaseError {
message: string,
cause: string
}
func query_database(query: string) {
throw("Connection timeout")
}
func fetch_user_data(user_id: int) {
try {
query_database("SELECT * FROM users WHERE id = " + user_id)
} except db_err {
error := DatabaseError {
message: "Failed to fetch user data for ID " + user_id,
cause: db_err.message
}
throw(error)
}
}
try {
fetch_user_data(123)
} except err {
print("Application error: " + err.message)
}
print("")
# ==============================================================================
# Example 5: Multiple Error Types
# ==============================================================================
print("--- Example 5: Multiple Error Types ---")
struct NetworkError {
message: string,
status: int
}
struct ParseError {
message: string,
data: string
}
func process_request(simulate_network: bool, simulate_parse: bool) {
if simulate_network {
error := NetworkError {
message: "Failed to connect to server",
status: 500
}
throw(error)
}
if simulate_parse {
error := ParseError {
message: "Invalid JSON format",
data: "{invalid}"
}
throw(error)
}
return "Success"
}
try {
process_request(true, false)
} except err {
print("Network error: " + err.message)
}
try {
process_request(false, true)
} except err {
print("Parse error: " + err.message)
}
try {
result := process_request(false, false)
print("Request succeeded: " + result)
} except err {
print("This won't print")
}
print("")
# ==============================================================================
# Example 6: Error Recovery Pattern
# ==============================================================================
print("--- Example 6: Error Recovery ---")
func safe_divide(a: float, b: float) {
if b == 0.0 {
throw("Division by zero")
}
return a / b
}
operations := [[10.0, 2.0], [20.0, 0.0], [15.0, 3.0], [8.0, 0.0]]
successful := 0
failed := 0
i := 0
while i < len(operations) {
op := operations[i]
a := op[0]
b := op[1]
try {
result := safe_divide(a, b)
print("✓ " + a + " / " + b + " = " + result)
successful := successful + 1
} except err {
print("✗ " + a + " / " + b + " failed: " + err.message)
failed := failed + 1
}
i := i + 1
}
print("Summary: " + successful + " succeeded, " + failed + " failed")
print("")
# ==============================================================================
# Example 7: Nested Try-Except
# ==============================================================================
print("--- Example 7: Nested Try-Except ---")
func risky_inner() {
throw("Inner error")
}
func risky_outer() {
throw("Outer error")
}
try {
print("Outer try block started")
try {
print("Inner try block started")
risky_inner()
} except inner_err {
print("Caught inner error: " + inner_err.message)
}
print("Between try-except blocks")
risky_outer()
} except outer_err {
print("Caught outer error: " + outer_err.message)
}
print("")
# ==============================================================================
# Example 8: Real-World Scenario - User Registration
# ==============================================================================
print("--- Example 8: User Registration Validation ---")
struct User {
username: string,
email: string,
age: int
}
func validate_username(username: string) {
if len(username) < 3 {
throw("Username must be at least 3 characters long")
}
if len(username) > 20 {
throw("Username cannot exceed 20 characters")
}
}
func validate_user_email(email: string) {
if len(email) == 0 {
throw("Email is required")
}
if !contains(email, "@") {
throw("Email must contain @ symbol")
}
if !contains(email, ".") {
throw("Email must contain a domain")
}
}
func validate_user_age(age: int) {
if age < 13 {
throw("User must be at least 13 years old")
}
if age > 120 {
throw("Age must be realistic")
}
}
func create_user(username: string, email: string, age: int) {
validate_username(username)
validate_user_email(email)
validate_user_age(age)
user := User {
username: username,
email: email,
age: age
}
return user
}
# Test Case 1: Invalid username
print("Test 1: Invalid username")
try {
user := create_user("ab", "test@example.com", 25)
print("User created successfully")
} except err {
print("Registration failed: " + err.message)
}
# Test Case 2: Invalid email
print("Test 2: Invalid email")
try {
user := create_user("john_doe", "invalid-email", 25)
print("User created successfully")
} except err {
print("Registration failed: " + err.message)
}
# Test Case 3: Invalid age
print("Test 3: Invalid age")
try {
user := create_user("john_doe", "john@example.com", 10)
print("User created successfully")
} except err {
print("Registration failed: " + err.message)
}
# Test Case 4: Valid user
print("Test 4: Valid user")
try {
user := create_user("john_doe", "john@example.com", 25)
print("✓ User created successfully!")
print(" Username: john_doe")
print(" Email: john@example.com")
print(" Age: 25")
} except err {
print("Registration failed: " + err.message)
}
print("")
print("==================================================")
print("All examples completed successfully!")
print("==================================================")