mirror of
https://github.com/Textualize/rich.git
synced 2025-09-06 18:30:38 +00:00
Add exception traceback example
This commit is contained in:
parent
cbac633ab3
commit
79547da217
1 changed files with 35 additions and 0 deletions
35
examples/exception.py
Normal file
35
examples/exception.py
Normal file
|
@ -0,0 +1,35 @@
|
|||
"""
|
||||
Basic example to show how to print an traceback of an exception
|
||||
"""
|
||||
|
||||
from rich.console import Console
|
||||
from rich.panel import Panel
|
||||
|
||||
console = Console()
|
||||
|
||||
|
||||
def zero(number: int) -> int:
|
||||
same_number = number
|
||||
result = same_number / 0
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
console.print(Panel("[i] Print exception traceback[/i]"))
|
||||
try:
|
||||
zero(10)
|
||||
except:
|
||||
console.print_exception()
|
||||
console.print("[red]Exception catched")
|
||||
|
||||
console.print(
|
||||
Panel(
|
||||
"[i] Print exception traceback with 5 extra lines and locals[/i]",
|
||||
style="yellow",
|
||||
)
|
||||
)
|
||||
try:
|
||||
zero(20)
|
||||
except:
|
||||
console.print_exception(extra_lines=5, show_locals=True)
|
||||
console.print("[red]Exception also catched")
|
Loading…
Add table
Add a link
Reference in a new issue