Adding chapter 4 source code

This commit is contained in:
muassif
2021-02-21 19:22:50 +04:00
committed by GitHub
parent 801ff51fdc
commit e8469ec365
32 changed files with 405 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
#exception3.py
import math
class NumTypeError(TypeError):
pass
class NegativeNumError(Exception):
def __init__(self):
super().__init__("Negative number not supported")
def sqrt(num):
if not isinstance(num, (int, float)) :
raise NumTypeError("only numbers are allowed")
if num < 0:
raise NegativeNumError
return math.sqrt(num)
if __name__ == "__main__":
try:
print(sqrt(9))
print(sqrt('a'))
print (sqrt(-9))
except NumTypeError as e:
print(e)
except NegativeNumError as e:
print(e)