best practices to manage errors with blender python ?

Hi,

I wonder what is the best practices to manage errors in a script function and especially if the type of an object is not the one expected by the function parameters ?

Something like that:


def myfunction(var)
    if not type(var)==int:
        pass # do something here for errors
    return None

Can I use “raise” for example ?
How do you do that ?

Thanks
++

You could use the try - except built in python methods.

Or just throw an exception and/or use the reports module if calling from an operator.

Thanks for the answer

I prefer do not use the try-except as long as possible because that slow down the scripts, but

> just throw an exception
Do you mean: http://docs.python.org/2/reference/simple_stmts.html#the-raise-statement ?

What is the report module ?

++

EDIT: I found this exception http://docs.python.org/3/library/exceptions.html#TypeError

raise TypeError('Integer value expected')

Currently, I have that:


def myfunction(var):
    if not isinstance(var,int):
        raise TypeError('Integer value expected for argument <var>. Got '+str(type(var)))
    else:
        print("integer=%i" % (var))
    return None

myfunction(var=2)
myfunction(var='a')

I replaced type(var)==int by isinstance(var,int) which seems better.
What do you think of this code ?
++

It seems a bit too much for me. Error trapping is an art and a science that can unnecessarily complicate your code. Sometimes you need a general function, but if it were me I would just make sure I don’t pass a string to a function that expects and int. User input is your enemy. Strict evaluation of user input is the key to making sure bad data does not get passed down the pipe to other function expecting certain variable types. Explicit conversion can be used. I use try/catch when I know an error might occur and I have to trap for it. It does not seem to slow my scripts down. The last thing you want to do, IMHO, is raise an error. Errors should never occur because they stop the script. The script should always continue even with bad or unexpected input.

For instance…



def myfunction(var):
    if not isinstance(var,int):
        raise TypeError('Integer value expected for argument <var>. Got '+str(type(var)))
    else:
        print("integer=%i" % (var))
    return None

myfunction(var=2)
myfunction(var=3.141)

This will cause your script to quit running. So even though you have attempted to trap the error you still end up in a crashed state. All the end user will experience is a crash, they don’t care whats under the hood.

I understand your approach! Off course, if I have to create a script destined to the final user, bad data will not be send internally between the functions. I have just to test if the input data are of the right type. If not, I will just send a message on the screen (bad user… wchoo-tisshh wchooooooo-tissssshhhh) and quit the script normally.

But if I give a code snippet to others programmers, the error trapping could take his meaning. The case is really different. Others programmers could use the function and they don’t always clearly understand the right type to use as input (especially if they skim the doc partially). Receiving a clear message in this case could be a good help.

Of course, if I not careful of the errors, the script will also end with a crash… Which is the same than controlled error :-/

So… finally we don’t have to be careful of the internal errors in a script ?