[Tutor] Understanding Custom Exception

Alex metallourlante at gmail.com
Tue Aug 31 13:40:54 CEST 2010


Hi all.

I'm working on a thin wrapper around the unix crontab command (just the
command, non the cron service).  The module will do some simple operation
like:
- Read the crontab file
- Adding a job to crontab identified by an id
- Deleting a job
- Deleting all jobs added

Now I have two questions:

1. Does it make sense to create a couple of custom exceptions like:

   class CrontabDoesNotExist(Exception): pass
   class CrontabSyntaxError(Exception): pass

  Or I should use builtin exception?

2. Does it make sense to catch an exception raised by a function just to
raise your own custom exception? For instance

subprocess.check_call(['crontab','-l'])

will raise a CalledProcessError exception if the crontab of the current user
does not exist. Does it make sense to catch the exception just to raise my
own custom exception? For instance:

try:
    subprocess.check_call(['crontab','-l'])
except CalledProcessError:
    raise CrontabDoesNotExist()

Maybe I could also make the CrontabDoesNotExist exception more useful
printing a message to inform the user that the crontab does not exist for
the current user.

Thanks in advance.

Alex


More information about the Tutor mailing list