From 3a195b2260661d100381a1ab93aa7158d443af69 Mon Sep 17 00:00:00 2001 From: Hizenberg469 Date: Thu, 20 Mar 2025 21:02:12 +0530 Subject: [PATCH] Error handling --- error-handling/error_handling.py | 18 +++++++++++ error-handling/wiki.txt | 53 ++++++++++++++++++++++++++++++++ generators/wiki.txt | 0 3 files changed, 71 insertions(+) create mode 100644 error-handling/error_handling.py create mode 100644 error-handling/wiki.txt create mode 100644 generators/wiki.txt diff --git a/error-handling/error_handling.py b/error-handling/error_handling.py new file mode 100644 index 0000000..7cf10ce --- /dev/null +++ b/error-handling/error_handling.py @@ -0,0 +1,18 @@ +# Error Handling + +while True: + try: + age = int(input('what is your age? ')) + 10/age + except ValueError: + print('please enter a number') + continue + except ZeroDivisionError: + print('please enter age higher than 0') + break + else: + print('thank you!') + break + finally: + print('ok, I\' finally done') + print('can you hear me?') \ No newline at end of file diff --git a/error-handling/wiki.txt b/error-handling/wiki.txt new file mode 100644 index 0000000..355da34 --- /dev/null +++ b/error-handling/wiki.txt @@ -0,0 +1,53 @@ +-> Error handling in python: + + for ex: + while True: + try: + age = int(input('what is your age? ')) + print(age) + except: + print('please enter a number') + else: + print('thank you') + break + + + for ex: + def sum(num1, num2): + try: + return num1 + num2 + except TypeError as err: + print(f'please enter a number {err}') + + + print(sum(1, '2')) + + for ex: + def sum(num1, num2): + try: + return num1/num2 + except (TypeError, ZeroDivisionError): + print(f'please enter a number {err}') + + + print(sum(1, '2')) + +-> How to throw your own error? + + for ex: + while True: + try: + age = int(input('what is your age? ')) + 10/age + raise ValueError('hey cut it out') + except ValueError: + print('please enter a valid number') + except ZeroDivisionError: + print('please enter age higher than 0') + break + else: + print('thank you') + break + finally: + print('ok, I\'m finally done') + print('can you hear me?') \ No newline at end of file diff --git a/generators/wiki.txt b/generators/wiki.txt new file mode 100644 index 0000000..e69de29