I have a project that I need to run and have no idea how to implement custom exceptions. It mostly does complicated scientific functions, to be vague.
Mostly it will be raising exceptions if something is not set. I've been given this as a starting example from runnables.
# Define a class inherit from an exception type
class CustomError(Exception):
def __init__(self, arg):
# Set some exception infomation
self.msg = arg
try:
# Raise an exception with argument
raise CustomError('This is a CustomError')
except CustomError, arg:
# Catch the custom exception
print 'Error: ', arg.msg
I have no idea how this is meant to work or how I am meant to implement my code. It's not very explicit.
To give an idea of a basic exception that needs created.
In a function:
if self.humidities is None:
print "ERROR: Humidities have not been set..."
return
Apparently this needs to raise/throw an exception instead.
__init__
in the constructor) though. – skyking Aug 14 '15 at 8:47class CustomError(Exception)
implements the custom exception,raise CustomError()
raises it,except CustomError
catches it. What exactly is unclear in this? What do you know, what don't you know? – deceze♦ Aug 14 '15 at 8:50Exception
, and then you raise it - basically as the example shows. For an explaination please read the official tutorial: docs.python.org/3.5/tutorial – skyking Aug 14 '15 at 9:00