i want to be able to raise exception like the built-in ones, you can either pass a message along with it, or just raise the class name(instance created implicitly). like this
raise IndexError
raise IndexError('Something went wrong, bro')
is my following code correct ?
initially i'm thinking about this:
class SomeError(Exception):
def __init__(self, *args):
if args:
print ' '.join(args)
class SomeCriticalError(SomeError):
def __init__(self, *args):
super(SomeCriticalError,self).__init__(*args)
print 'This is a critical error, process stopped ...'
sys.exit(1)
class SomeCookiesError(SomeCriticalError):
def __init__(self, *args):
print 'Firefox cookies retrieving failed ..'
super(SomeCookiesError,self).__init__(*args)
but in this case, i have to define __init__() for every subclass of CmiCriticalError, i felt something must be wrong ( calling super() in each subclass seems awkward ), then i tried this
class SomeError(Exception):
def __init__(self, *args):
if args:
print ' '.join(args)
else:
print self
class SomeCriticalError(SomeError):
def __init__(self, *args):
super(SomeCriticalError,self).__init__(*args)
print 'This is a critical error, process stopped ...'
sys.exit(1)
class SomeCookiesError(SomeCriticalError):
def __str__(self):
return 'Firefox cookies retrieving failed ..'
but this still feels awkward, what am i missing ?
basically what i want to do is handle exception in the exception instance itself, like the sys.exit() in SomeCriticalError, and because in my main code, i may raise SomeCookiesError at multiple places, i don't want to pass the same message string argument each time, so can i do what the built in exceptions do ? ( when no argument is passed, print default message )
sys.exit()in except right ? and avoid using exceptions in this case. but i may need CmiCookiesError in multiple places of my code, say 10 places. so use try/except at these 10 places ?except YourException:rather than inYourException); if not, raise a standard exception and handle it where it is not ambiguous.