Repository navigation
Expand file tree
/
Copy pathtry_except.py
More file actions
71 lines (56 loc) · 2.12 KB
/
Copy pathtry_except.py
File metadata and controls
71 lines (56 loc) · 2.12 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
"""`try` / `except` / `else` / `finally` — handling what goes wrong.
Python's culture is EAFP: "easier to ask forgiveness than permission". Try the
operation and handle the failure, rather than testing every precondition first
(LBYL) — the test can go stale between the check and the use anyway.
Four blocks, each with a job:
try the code that might fail
except runs only on a matching exception
else runs only if try succeeded — keep it small, it is not protected
finally always runs, even on return or an unhandled raise: cleanup goes here
"""
def parse_port(raw: str) -> int:
try:
port = int(raw)
except ValueError:
print(f" {raw!r} is not a number, defaulting to 8080")
return 8080
else:
print(f" parsed {port}")
return port
finally:
print(" (finally always runs)")
def main() -> None:
parse_port("9000")
parse_port("nine thousand")
# Catch specific exceptions. A bare `except:` also swallows KeyboardInterrupt
# and SystemExit, and `except Exception:` hides bugs you wanted to see.
data = {"a": 1}
for key in ("a", "b"):
try:
print(f"data[{key!r}] = {data[key]}")
except KeyError as exc:
print(f"missing key: {exc}")
# Several types in one handler, and the exception object itself.
for value in ("10", "0", "x"):
try:
print(f"100/{value} = {100 / int(value)}")
except (ValueError, ZeroDivisionError) as exc:
print(f"{type(exc).__name__}: {exc}")
# EAFP vs LBYL.
def lbyl(d: dict[str, int], k: str) -> int:
return d[k] if k in d else 0 # two lookups
def eafp(d: dict[str, int], k: str) -> int:
try:
return d[k]
except KeyError:
return 0
print(f"lbyl={lbyl(data, 'z')} eafp={eafp(data, 'z')}")
# finally runs even when the try block returns.
def cleanup_demo() -> str:
try:
return "returned from try"
finally:
print(" cleanup ran before the value was handed back")
print(cleanup_demo())
if __name__ == "__main__":
main()