Debian Codegraph @

examples/exception.py

raw
1 """
2 Basic example to show how to print an traceback of an exception
3 """
4 from typing import List, Tuple
5
6 from rich.console import Console
7
8 console = Console()
9
10
11 def divide_by(number: float, divisor: float) -> float:
12 """Divide any number by zero."""
13 # Will throw a ZeroDivisionError if divisor is 0
14 result = number / divisor
15 return result
16
17
18 def divide_all(divides: List[Tuple[float, float]]) -> None:
19 """Do something impossible every day."""
20
21 for number, divisor in divides:
22 console.print(f"dividing {number} by {divisor}")
23 try:
24 result = divide_by(number, divisor)
25 except Exception:
26 console.print_exception(extra_lines=8, show_locals=True)
27 else:
28 console.print(f" = {result}")
29
30
31 DIVIDES = [
32 (1000, 200),
33 (10000, 500),
34 (1, 0),
35 (0, 1000000),
36 (3.1427, 2),
37 (888, 0),
38 (2**32, 2**16),
39 ]
40
41 divide_all(DIVIDES)