@@ -16,11 +16,7 @@ def raising_exception_groups():
1616 exceptions. You create it with a message and a sequence of exceptions.
1717 """
1818 # Create a simple exception group with multiple exceptions
19- exceptions = [
20- ValueError ("Invalid value" ),
21- TypeError ("Wrong type" ),
22- KeyError ("Missing key" )
23- ]
19+ exceptions = [ValueError ("Invalid value" ), TypeError ("Wrong type" ), KeyError ("Missing key" )]
2420
2521 try :
2622 # Raise an ExceptionGroup containing multiple exceptions
@@ -34,7 +30,6 @@ def raising_exception_groups():
3430 return "Caught exception group"
3531
3632
37-
3833def handling_with_except_star ():
3934 """Demonstrate the except* syntax for handling exception groups.
4035
@@ -44,11 +39,7 @@ def handling_with_except_star():
4439 results = []
4540
4641 try :
47- exceptions = [
48- ValueError ("Invalid value 1" ),
49- ValueError ("Invalid value 2" ),
50- TypeError ("Wrong type 1" )
51- ]
42+ exceptions = [ValueError ("Invalid value 1" ), ValueError ("Invalid value 2" ), TypeError ("Wrong type 1" )]
5243 raise ExceptionGroup ("Processing errors" , exceptions )
5344 except* ValueError as eg :
5445 # This catches all ValueError instances from the group
@@ -72,12 +63,7 @@ def handling_multiple_types():
7263 results = []
7364
7465 try :
75- exceptions = [
76- ValueError ("Bad value" ),
77- TypeError ("Bad type" ),
78- RuntimeError ("Runtime issue" ),
79- ValueError ("Another bad value" )
80- ]
66+ exceptions = [ValueError ("Bad value" ), TypeError ("Bad type" ), RuntimeError ("Runtime issue" ), ValueError ("Another bad value" )]
8167 raise ExceptionGroup ("Various errors" , exceptions )
8268 except* ValueError as eg :
8369 # Handles all ValueErrors (there are 2)
@@ -100,12 +86,7 @@ def nested_exception_groups():
10086 unless you explicitly unwrap them.
10187 """
10288 # Create a flat exception group with mixed exception types
103- exceptions = [
104- ValueError ("Value error 1" ),
105- ValueError ("Value error 2" ),
106- TypeError ("Type error 1" ),
107- RuntimeError ("Runtime error" )
108- ]
89+ exceptions = [ValueError ("Value error 1" ), ValueError ("Value error 2" ), TypeError ("Type error 1" ), RuntimeError ("Runtime error" )]
10990
11091 eg = ExceptionGroup ("Multiple errors" , exceptions )
11192
@@ -134,13 +115,9 @@ def partial_handling():
134115
135116 try :
136117 try :
137- exceptions = [
138- ValueError ("Value error" ),
139- TypeError ("Type error" ),
140- KeyError ("Key error" )
141- ]
118+ exceptions = [ValueError ("Value error" ), TypeError ("Type error" ), KeyError ("Key error" )]
142119 raise ExceptionGroup ("Mixed errors" , exceptions )
143- except* ValueError as eg :
120+ except* ValueError :
144121 # Only handle ValueError, leaving TypeError and KeyError
145122 results .append ("Handled ValueError" )
146123 # The other exceptions are automatically re-raised
@@ -160,12 +137,7 @@ def filtering_exceptions():
160137
161138 The subgroup() method allows you to filter exceptions by type.
162139 """
163- exceptions = [
164- ValueError ("Value 1" ),
165- TypeError ("Type 1" ),
166- ValueError ("Value 2" ),
167- RuntimeError ("Runtime 1" )
168- ]
140+ exceptions = [ValueError ("Value 1" ), TypeError ("Type 1" ), ValueError ("Value 2" ), RuntimeError ("Runtime 1" )]
169141
170142 eg = ExceptionGroup ("All errors" , exceptions )
171143
@@ -192,6 +164,7 @@ def practical_concurrent_example():
192164 This demonstrates a common use case: running multiple tasks
193165 where each can fail independently.
194166 """
167+
195168 def process_user (user_id ):
196169 """Simulate processing a user."""
197170 if user_id == 1 :
@@ -303,11 +276,7 @@ def split_by_type(eg):
303276 type_errors = eg .subgroup (TypeError )
304277 return value_errors , type_errors
305278
306- exceptions = [
307- ValueError ("Value 1" ),
308- TypeError ("Type 1" ),
309- ValueError ("Value 2" )
310- ]
279+ exceptions = [ValueError ("Value 1" ), TypeError ("Type 1" ), ValueError ("Value 2" )]
311280 eg = ExceptionGroup ("Mixed" , exceptions )
312281 values , types = split_by_type (eg )
313282
@@ -325,11 +294,7 @@ def split_by_type(eg):
325294 def simulate_async_failures ():
326295 """Simulate collecting errors from multiple async operations."""
327296 # In real async code, you might use asyncio.gather with return_exceptions=True
328- task_errors = [
329- TimeoutError ("Task 1 timed out" ),
330- ValueError ("Task 2 invalid input" ),
331- ConnectionError ("Task 3 connection failed" )
332- ]
297+ task_errors = [TimeoutError ("Task 1 timed out" ), ValueError ("Task 2 invalid input" ), ConnectionError ("Task 3 connection failed" )]
333298
334299 results = {"timeout" : 0 , "value" : 0 , "connection" : 0 }
335300
0 commit comments