Skip to content

Commit

Permalink
Add example of chaining exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ABDreos committed Jan 30, 2024
1 parent c7095dc commit 18ae792
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions 2024/tuesday_tips/custom_exceptions/exception_chaining.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def convert_to_integer(text: str) -> int:
try:
return int(text)
except ValueError as e:
raise TypeError("Conversion to integer failed") from e


def main() -> None:
try:
print(convert_to_integer("abc"))
except Exception as e:
print(f"An error occurred: {e}")
print(f"Cause of the error: {e.__cause__}")


if __name__ == "__main__":
main()

0 comments on commit 18ae792

Please sign in to comment.