Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9c7641f

Browse files
committed
Lecture 12
1 parent a402b4a commit 9c7641f

File tree

2 files changed

+269
-1
lines changed

2 files changed

+269
-1
lines changed

‎Lecture12.md‎

Lines changed: 269 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,272 @@ Plan:
1717
+ Podstawy wyjątków
1818
+ Szczegółowe informacje dotyczące wyjątków
1919
+ Obiekty wyjątków
20-
+ Projektowanie z użyciem wyjątków
20+
+ Projektowanie z użyciem wyjątków
21+
22+
23+
# Podstawy wyjątków
24+
25+
Wyjątki w Pythonie służą do modifikacji przebiegu sterowania w programie. Wyjątęk nie zawsze oznacza błąd.
26+
27+
28+
# Instrukcje wyjątków
29+
30+
Instrukcje wyjątków:
31+
32+
- **try/except**
33+
- **try/finally**
34+
- **raise**
35+
- **assert**
36+
- **with/as**
37+
38+
39+
# Role wyjątków
40+
41+
Role wyjątków:
42+
43+
- Obsługa błędów
44+
- Powiadomienie o zdarzeniach
45+
- Obsługa przypadków specjalnych
46+
- Działanie końcowe
47+
- Niezwykły przebieg sterowania
48+
49+
50+
# Domyślny program obsługi wyjątków
51+
52+
```python
53+
def fetcher(obj, index):
54+
return obj[index]
55+
x = 'abcd'
56+
fetcher(x, 8) # Traceback ... IndexError
57+
```
58+
59+
60+
# Przechwytywanie wyjątków
61+
62+
```python
63+
try:
64+
fetcher(x, 8)
65+
except IndexError:
66+
print("Przechwycono wyjątek")
67+
```
68+
69+
70+
# Zgłaszanie wyjątków
71+
72+
```python
73+
try:
74+
raise IndexError
75+
except IndexError:
76+
print("Przechwycono wyjątek")
77+
```
78+
79+
80+
# Zgłaszanie wyjątków
81+
82+
```python
83+
assert False, "Assert error here!"
84+
# AssertionError: Assert error here!
85+
```
86+
87+
88+
# Definiowanie wyjątków
89+
90+
```python
91+
class Bad(Exception):
92+
pass
93+
def doomed():
94+
raise Bad()
95+
try:
96+
doomed()
97+
except Bad:
98+
print("Przechwycenie Bad")
99+
```
100+
101+
102+
# Działania końcowe
103+
104+
```python
105+
def after():
106+
try:
107+
fetcher(x, 8)
108+
finally:
109+
print("Po fetcher")
110+
print("Po try")
111+
after() # Po fetcher; IndexError
112+
```
113+
114+
115+
# Mieszanie instrukcji
116+
117+
```python
118+
try:
119+
assert False, "false"
120+
except:
121+
print("except")
122+
else:
123+
print("else")
124+
finally:
125+
print("finally")
126+
```
127+
128+
129+
# Pełna forma instrukcji try
130+
131+
```python
132+
try:
133+
<instrukcje>
134+
except <nazwa1>:
135+
<instrukcje>
136+
except (nazwa2, nazwa3):
137+
<instrukcje>
138+
except <nazwa4> as dane:
139+
<instrukcje>
140+
else:
141+
<instrukcje>
142+
finally:
143+
<instrukce>
144+
```
145+
146+
147+
# Instrukcja raise
148+
149+
```python
150+
raise <instancja>
151+
raise <klasa> # niejawne wywołanie klasa()
152+
raise # ponowne zgłoszenie ostatniego wyjątku
153+
exc = IndexError()
154+
raise exc
155+
```
156+
157+
158+
# Dostęp do instancji wyjątku
159+
160+
```python
161+
try:
162+
...
163+
except IndexError as X:
164+
... # użycie X
165+
```
166+
167+
168+
# Łańcuchy wyjątków Pythonie
169+
170+
```python
171+
try:
172+
1 / 0
173+
except Exception as E:
174+
raise TypeError('Źle!') from E
175+
```
176+
177+
178+
# Instrukcja assert
179+
180+
```python
181+
assert <test>, <dane> # <dane> opcjonalnie
182+
if __debug__: # python -O main.py - zmienia debug na False
183+
if not <test>:
184+
raise AssertionError(<dane>)
185+
```
186+
187+
188+
# Menedżery kontekstu
189+
190+
```python
191+
with wyrażenie [as zmienna]:
192+
blok_with
193+
```
194+
195+
196+
# Menedżery kontekstu
197+
198+
```python
199+
with open('file.txt') as in_file:
200+
for line in in_file:
201+
print(line)
202+
```
203+
204+
205+
# Menedżery kontekstu
206+
207+
```python
208+
in_file = open('file.txt')
209+
try:
210+
for line in in_file:
211+
print(line)
212+
finally:
213+
in_file.close()
214+
```
215+
216+
217+
# Menedżery kontekstu
218+
219+
```python
220+
class TraceBlock:
221+
def message(self, arg):
222+
print(f"in message {arg}")
223+
def __enter__(self):
224+
return self
225+
def __exit__(self, exc_type, exc_value, exc_tb):
226+
if exc_type is None:
227+
print("normalne wyjście")
228+
else:
229+
print("reraising error")
230+
return False # tylko jeśli False
231+
```
232+
233+
234+
# Menedżery kontekstu
235+
236+
```python
237+
with TraceBlock as action:
238+
action.message()
239+
with TraceBlock as action:
240+
action.message()
241+
raise TypeError
242+
```
243+
244+
245+
# Obiekty wyjątków
246+
247+
Obiekty wyjątków:
248+
249+
- Można je organizować w kategorie
250+
- Dołączają informacje o stanie
251+
- Obsługują dziedziczenie
252+
253+
254+
# Kategorie wyjątków
255+
256+
```python
257+
class General(Exception): pass
258+
class Specific1(General): pass
259+
class Specific2(General): pass
260+
def raiser0():
261+
raise General
262+
def raiser1():
263+
raise Specific1
264+
def raiser2():
265+
raise Specific2
266+
```
267+
268+
269+
# Kategorie wyjątków
270+
271+
```python
272+
for func in [raiser0, raiser1, raiser2]:
273+
try:
274+
func()
275+
except General:
276+
print("Przechwycono")
277+
```
278+
279+
280+
# Stan wyjątku
281+
282+
```python
283+
try:
284+
raise IndexError('abc')
285+
except IndexError as E:
286+
print(E.args)
287+
```
288+

‎Lecture12.pdf‎

171 KB
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /