-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtryiexcept.py
More file actions
54 lines (42 loc) · 1.17 KB
/
tryiexcept.py
File metadata and controls
54 lines (42 loc) · 1.17 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
"""
Osbługa wyjątków
try: - chce coś zrobić
print(x)
jak x nie istnieje to nie przewie dzialania programu, przekaze go do exception
except Exception as exc: #blad w exc sie zapisze i nie zatrzyma programu
print(exc)
except TypeError exc #8//"b" to type error
except ZeroDivisionError as exc #8/0 to ten przypadek
/\
|
przewidywane błedy
nieprzewidywany błąd:
except Exception as exc:
finally:
pass
jak już ci nadzieji zabraknie
raise TypeError powoduje wywołanie błędu danego typu. W tym wypadku TypeError
najczęściej wykorzystywane w funkcjach
np.
def dod(a,b):
#jezeli typ jest niepoprawny
raise TypeError
Program, story prosi o podanie 2 liczb i wykonuje dzielenie i obsługuje błędy
"""
while True:
print('Podaj liczy do dzielenia')
x = input()
y = input()
try:
x = float(x)
y = float(y)
z = x / y
print(z)
except TypeError:
print('Wygrałeś')
except ZeroDivisionError:
print('Dzielenie przez zero')
except ValueError:
print('To nie jest liczba')
else:
print('Wsystko działa')