Skip to content

Commit afee487

Browse files
committed
exceptions
1 parent c227c9f commit afee487

File tree

13 files changed

+156
-0
lines changed

13 files changed

+156
-0
lines changed

3_exceptions-and-libs/exc1.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
try:
2+
x = int(input("Enter x: "))
3+
print(f"You entered {x}")
4+
except ValueError:
5+
print("Oops x isn't an Integer")

3_exceptions-and-libs/exc10.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
class HttpException(Exception):
3+
statusCode = None
4+
message = None
5+
def __init__(self):
6+
super().__init__(f"Status Code: {self.statusCode}! Message: {self.message}")
7+
8+
class NotFoundError(HttpException):
9+
statusCode = 404
10+
message = "Server Not Found"
11+
12+
class ServerError(HttpException):
13+
statusCode = 500
14+
message = "Server is down! We shall soon be vailable"
15+
16+
def raiseNotFoundError():
17+
raise NotFoundError()
18+
19+
raiseNotFoundError()

3_exceptions-and-libs/exc2.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
try:
2+
x = int(input("Enter x: ")) # x will not be created if it isn't any integer
3+
except ValueError:
4+
print("Oops x isn't an Integer")
5+
else:
6+
print(f"You entered {x}")

3_exceptions-and-libs/exc3.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
while True:
2+
try:
3+
x = int(input("Enter x: ")) # x will not be created if it isn't any integer
4+
except ValueError:
5+
print("Oops x isn't an Integer")
6+
else:
7+
break
8+
print(f"You entered {x}")

3_exceptions-and-libs/exc4.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def main():
2+
z = get_int()
3+
print(f"You entered {z}")
4+
5+
def get_int():
6+
while True:
7+
try:
8+
return int(input("Enter x: ")) # x will not be created if it isn't an integer
9+
except ValueError:
10+
print("Oops x isn't an Integer")
11+
12+
main()

3_exceptions-and-libs/exc5.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
# try ... except .... else ... finally
3+
4+
try:
5+
nume = int(input("Enter the numerator: "))
6+
denom = int(input("Enter the denominator: "))
7+
quotient = nume/denom
8+
print("Correct division")
9+
except ZeroDivisionError:
10+
print("You can't divide by zero")
11+
except ValueError:
12+
print("Only integers required")
13+
else:
14+
print("Quotient =", quotient)
15+
finally:
16+
print("I am always here")

3_exceptions-and-libs/exc6.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
num1 = int(input("Num 1: "))
2+
num2 = int(input("Num 2: "))
3+
4+
if num2 == 0:
5+
raise ZeroDivisionError("No division by zero")
6+
else:
7+
print("Quotient =",num1/num2)

3_exceptions-and-libs/exc7.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Create your custom exceptions
2+
# - clean code
3+
# - documentation
4+
# - set yourself away from others
5+
6+
class AgeErrorException(Exception): # inheritance
7+
def __init__(self, value):
8+
self.value = value
9+
10+
try:
11+
age = int(input("What is your age?:"))
12+
if age < 0 or age >=80:
13+
raise AgeErrorException("Out of range age")
14+
print(f"You are {age} years OLD")
15+
except ValueError:
16+
print("We accept only integers")
17+
except AgeErrorException as ageErr:
18+
print(ageErr)

3_exceptions-and-libs/exc8.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Create your custom exceptions
2+
# - clean code
3+
# - documentation
4+
# - set yourself away from others
5+
6+
class AgeErrorException(Exception): # inheritance
7+
def __init__(self, value, message="Age out of range"):
8+
self.value = value
9+
self.message = message
10+
super().__init__(self.message)
11+
12+
age = int(input("What is your age? "))
13+
if not 0 < age < 80:
14+
raise AgeErrorException(age)

3_exceptions-and-libs/exc9.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class HallAllocationError(Exception):
2+
pass
3+
4+
class HomeDistanceError(Exception):
5+
pass
6+
7+
def checkHomeDist(distance):
8+
maxDistance = 80
9+
if distance < maxDistance:
10+
raise HomeDistanceError("You can commute from home")
11+
else:
12+
print("You may be allocated a hall")
13+
14+
def checkAge(age):
15+
ageLimit = 30
16+
if age > ageLimit:
17+
raise HallAllocationError("You are too old to be allocated a hall")
18+
else:
19+
dist = int(input("Distance = "))
20+
checkHomeDist(dist)
21+
22+
try:
23+
age = int(input("What is your age?"))
24+
checkAge(age)
25+
except HallAllocationError as haErr:
26+
print(haErr)
27+
except HomeDistanceError as hoErr:
28+
print(hoErr)

3_exceptions-and-libs/libs.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import sys
2+
3+
try:
4+
print("Hello there! I am", sys.argv[1])
5+
except IndexError:
6+
print("No arguments supplied")

3_exceptions-and-libs/libs2.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import sys
2+
3+
if len(sys.argv) < 3:
4+
sys.exit("Not enough args")
5+
6+
elif len(sys.argv) > 3:
7+
print("A lot of args")
8+
9+
else:
10+
print("Hello there! I am", sys.argv[1])

3_exceptions-and-libs/libs3.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import sys
2+
3+
if len(sys.argv) < 3:
4+
sys.exit("Not enough args")
5+
6+
for x in sys.argv[1:]:
7+
print("Hello there!", x)

0 commit comments

Comments
 (0)