Skip to content
This repository was archived by the owner on Apr 10, 2022. It is now read-only.

Commit dea7741

Browse files
committed
Move the "Unmatched exceptions" section up, as @iritkatriel suggested
1 parent 9cbc71a commit dea7741

File tree

1 file changed

+55
-57
lines changed

1 file changed

+55
-57
lines changed

except_star.md

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -106,59 +106,6 @@ InterruptedError
106106
BlockingIOError
107107
```
108108

109-
### Raising ExceptionGroups manually
110-
111-
Exception groups can be raised manually:
112-
113-
```python
114-
try:
115-
low_level_os_operation()
116-
except *OSerror as errors:
117-
new_errors = []
118-
for e in errors:
119-
if e.errno != errno.EPIPE:
120-
new_errors.append(e)
121-
raise ExceptionGroup(*new_errors)
122-
```
123-
124-
The above code ignores all `EPIPE` OS errors, while letting all other
125-
exceptions propagate.
126-
127-
Raising an `ExceptionGroup` introduces nesting:
128-
129-
```python
130-
try:
131-
raise ExceptionGroup(ValueError('a'), TypeError('b'))
132-
except *ValueError:
133-
raise ExceptionGroup(KeyError('x'), KeyError('y'))
134-
135-
# would result in:
136-
#
137-
# ExceptionGroup(
138-
# ExceptionGroup(
139-
# KeyError('x'),
140-
# KeyError('y'),
141-
# ),
142-
# TypeError('b'),
143-
# )
144-
```
145-
146-
Although a regular `raise Exception` would not wrap `Exception` in a group:
147-
148-
```python
149-
try:
150-
raise ExceptionGroup(ValueError('a'), TypeError('b'))
151-
except *ValueError:
152-
raise KeyError('x')
153-
154-
# would result in:
155-
#
156-
# ExceptionGroup(
157-
# KeyError('x'),
158-
# TypeError('b')
159-
# )
160-
```
161-
162109
### Unmatched Exceptions
163110

164111
Example:
@@ -186,11 +133,9 @@ and then terminate with an unhandled `ExceptionGroup`:
186133

187134
```
188135
ExceptionGroup(
136+
TypeError('b'),
137+
TypeError('c'),
189138
KeyError('e'),
190-
ExceptionGroup(
191-
TypeError('b'),
192-
TypeError('c')
193-
)
194139
)
195140
```
196141

@@ -241,6 +186,59 @@ except *BlockingIOError:
241186
# ExceptionGroup(BlockingIOError())
242187
```
243188

189+
### Raising ExceptionGroups manually
190+
191+
Exception groups can be raised manually:
192+
193+
```python
194+
try:
195+
low_level_os_operation()
196+
except *OSerror as errors:
197+
new_errors = []
198+
for e in errors:
199+
if e.errno != errno.EPIPE:
200+
new_errors.append(e)
201+
raise ExceptionGroup(*new_errors)
202+
```
203+
204+
The above code ignores all `EPIPE` OS errors, while letting all other
205+
exceptions propagate.
206+
207+
Raising an `ExceptionGroup` introduces nesting:
208+
209+
```python
210+
try:
211+
raise ExceptionGroup(ValueError('a'), TypeError('b'))
212+
except *ValueError:
213+
raise ExceptionGroup(KeyError('x'), KeyError('y'))
214+
215+
# would result in:
216+
#
217+
# ExceptionGroup(
218+
# ExceptionGroup(
219+
# KeyError('x'),
220+
# KeyError('y'),
221+
# ),
222+
# TypeError('b'),
223+
# )
224+
```
225+
226+
Although a regular `raise Exception` would not wrap `Exception` in a group:
227+
228+
```python
229+
try:
230+
raise ExceptionGroup(ValueError('a'), TypeError('b'))
231+
except *ValueError:
232+
raise KeyError('x')
233+
234+
# would result in:
235+
#
236+
# ExceptionGroup(
237+
# KeyError('x'),
238+
# TypeError('b')
239+
# )
240+
```
241+
244242
### Exception Chaining
245243

246244
If an error occurs during processing a set of exceptions in a `except *` block,

0 commit comments

Comments
 (0)