Creating your own Exceptions in Python

I needed to handle errors better so I looked into creating my own exceptions.  It easy, and this is a dead simple example.

#!/usr/bin/python

class AdditionError(Exception):
        pass

x = 2
y = 'i'

try:
   print  x + y

except:
   raise AdditionError("Couldn't add x + y ")
    
 
root@echo:/home/akonkol/Code/exception# ./addition.py 
Traceback (most recent call last): 
  File "./addition.py", line 14, in 
     raise AdditionError("Couldn't add x + y ") 
__main__.AdditionError: Couldn't add x + y
Tagged as exception , python
Written by Andrew Konkol on June 20th, 2012

0 Comments

Log in with Twitter, Google, Facebook, LinkedIn to leave a comment.