[Tutor] exception classes

Kevin McCormick kev@sat.net
Mon, 31 Dec 2001 20:51:23 -0600


dman wrote:

> On Mon, Dec 31, 2001 at 01:15:28PM -0600, Kevin McCormick wrote:
> | I am trying to figure out how to design an exception class
> 
> What does your exception need to be able to do and what info does it
> need to store?  The following is a minimal example :
> 
> # first define the class
> class MyException( Exception ) :
>     pass
>

I have seen this where a block goes something like:

try:
    <some operation>
except:
    raise MyException( 'error in some operation' )

 


For example:

   class MyException( Exception ):
       pass

   def f1(a, b, *var, **kw):
       print a, b, var, kw

   try:
       f1(1, 2, 34, 45, c=4, d=5, b=6)
   except:
       raise MyException('you made a boo-boo')

Gives result:
   Traceback (most recent call last):
     File "<stdin>", line 10, in ?
   __main__.MyException: you made a boo-boo

Now, what do you do with it?


I am writing a calendar type module of spreadsheet like functions, and I 
want to return -1 for calls made with bad arguments, but give some 
feedback, like -> error variable: x could not be processed, perhaps to a 
log file or something.

Also, as in my example, the Syntax Error was assigning 6 to b, where 2 
was in the b slot.  How can information of this type be preserved?

Thanks
Kevin