When i give wrong number of parameters in a function , i get errors. How do I handle it?
I gave
def fun_name(...):
try:
...
except TypeError:
print 'Wrong no of arg'
It is not working.
Help please.
When i give wrong number of parameters in a function , i get errors. How do I handle it?
I gave
def fun_name(...):
try:
...
except TypeError:
print 'Wrong no of arg'
It is not working.
Help please.
If you remove the try...catch
parts it should show you what kind of exception it is throwing.
The caller triggers this exception, not the receiver.
If you want the receiving function to explicitly check argument count you'll need to use varargs:
def fun_name(*args):
if len(args) != 2:
raise TypeError('Two arguments required')
You need to handle it where you call the function.
try:
fun_name(...)
except TypeError:
print "error!"
If you call a function with the wrong number of parameters then there are two possibilities: