Back
Close

Computing with Data

elgeish
33.5K views
Previous: Handling Exceptions - Part III Next: Docstrings

Handling Exceptions - Part III

import math
# Custom exception: either type or value error
class InvalidTypeOrValueError(Exception):
def __init__(self, a_val, a_type): # custom constructor
# calls super class constructor (Python 3 syntax)
super().__init__("incorrect type and value. " +
"val: " + str(a_val) + " " + "type: " + str(a_type))
self.val = a_val
self.type = a_type
def foo(x):
if not isinstance(x, int) or x<=0:
raise InvalidTypeOrValueError(x,type(x))
return(x + math.log(x))
try:
foo(-3)
foo(3)
except InvalidTypeOrValueError as e:
print("Incorrect type or value in sequence of foo() calls: ")
print(e.args)
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants