Chapter folders renamed
This commit is contained in:
@@ -0,0 +1 @@
|
||||
Hello WorldEnd
|
||||
@@ -0,0 +1,15 @@
|
||||
#exception1.py
|
||||
try:
|
||||
#print (x)
|
||||
x = 5
|
||||
y = 1
|
||||
z = x /y
|
||||
print('x'+y)
|
||||
|
||||
except NameError as e:
|
||||
print(e)
|
||||
except ZeroDivisionError:
|
||||
print("Division by 0 is not allowed")
|
||||
except Exception as e:
|
||||
print("An error occured")
|
||||
print(e)
|
||||
@@ -0,0 +1,10 @@
|
||||
#exception2.py
|
||||
try:
|
||||
f = open("abc.txt", "w")
|
||||
except Exception as e:
|
||||
print("Error:" + e)
|
||||
else:
|
||||
f.write("Hello World")
|
||||
f.write("End")
|
||||
finally:
|
||||
f.close()
|
||||
@@ -0,0 +1,18 @@
|
||||
#exception3.py
|
||||
import math
|
||||
def sqrt(num):
|
||||
|
||||
if not isinstance(num, (int, float)) :
|
||||
raise TypeError("only numbers are allowed")
|
||||
if num < 0:
|
||||
raise Exception ("Negative number not supported")
|
||||
|
||||
return math.sqrt(num)
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
print(sqrt(9))
|
||||
print(sqrt('a'))
|
||||
print (sqrt(-9))
|
||||
except Exception as e:
|
||||
print(e)
|
||||
@@ -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)
|
||||
Reference in New Issue
Block a user