Chapter folders renamed

This commit is contained in:
Karan Solanki
2021-08-13 11:44:51 +05:30
parent d9f3f5b159
commit 1eb709f83a
211 changed files with 0 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
Hello WorldEnd
+15
View File
@@ -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)
+10
View File
@@ -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()
+18
View File
@@ -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)
+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)