mirror of
https://github.com/Hizenberg469/Python-tutorial.git
synced 2026-04-19 15:12:22 +03:00
Error handling
This commit is contained in:
18
error-handling/error_handling.py
Normal file
18
error-handling/error_handling.py
Normal file
@@ -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?')
|
||||||
53
error-handling/wiki.txt
Normal file
53
error-handling/wiki.txt
Normal file
@@ -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?')
|
||||||
0
generators/wiki.txt
Normal file
0
generators/wiki.txt
Normal file
Reference in New Issue
Block a user