Not passing TypeError

def test(x,y,z):
try:
value = False
#Values must be integers
x = int(x)
y = int(y)
z = int(z)
print(“Correct values entered.”)
if x == y or x == z or y == z:
value = True

# Capture TypeError
except TypeError:
    print("TypeError: Please enter only numbers.")

#Capture
except ValueError:
    print("ValueError: Invalid values, only numbers allowed.")



#Capture any kind of errors
except Exception:
    print(Exception)
    print("Exception, Invalid values, only numbers allowed!")

#print entered values
finally:
    print("Values Entered:", x, y, z)
    return value

print(test(1))

Error received:
Traceback (most recent call last):
File “/Users/olicaluag/Downloads/errorhandling.py”, line 39, in
print(test(1))
TypeError: test() missing 2 required positional arguments: ‘y’ and ‘z’

Question:

  1. If I received TypeError message, why it is not triggering the “except TypeError:”?
  2. What possible mistake i can use to trigger “except Exception:”?

def test(x,y,z):
try:
[…]

Capture TypeError

except TypeError:
print(“TypeError: Please enter only numbers.”)
#Capture
except ValueError:
print(“ValueError: Invalid values, only numbers allowed.”)
#Capture any kind of errors
except Exception:
print(Exception)
print(“Exception, Invalid values, only numbers allowed!”)
#print entered values
finally:
print(“Values Entered:”, x, y, z)
return value

print(test(1))

Error received:
Traceback (most recent call last):
File “/Users/olicaluag/Downloads/errorhandling.py”, line 39, in
print(test(1))
TypeError: test() missing 2 required positional arguments: ‘y’ and ‘z’

Question:

  1. If I received TypeError message, why it is not triggering the “except TypeError:”?

Because the “except TypeError” is inside the test() function, which
never even gets run because you’ve called it incorrectly.

You can catch it where you’re doing the call:

try:
    test(1)
except TypeError as e:
    print("bad use of test()", e)
  1. What possible mistake i can use to trigger “except Exception:”?

Cause some other exception type. You just outright “raise OSError()” or
some other exception.

The question itself says why we usually do not catch “Exception”: it
might be anything, and you don’t know what to do about it.

The general rule for exceptions is that you should only catch exceptions
which (a) you expect - your question says you don’t expect anything else
and (b) which you know can be “handled” with some specific action so
that the rest of the programme makes sense.

You should not try to catch other exceptions because that prevents you
getting a nice stack tract which at least tells you (a) what the
exception was and (b) where is happened and the call stack which led you
there.

The purpose of try/except is not to make your programme never fall over
but to catch specific situations where the exceptional circumstance is
anticpated and has a meaningful response.

It may be helpful to think of exceptions as similar in purpose to
special return values indicating “not a valid result”. For example in C
or Go, where there are no exceptions, functions instead return various
“out of range” values. For example, many UNIX C API calls return -1 when
passed an invalid value. You’d catch that situation with an
if-statement.

The convenience of an exception is that you can decide where to catch
it. If you don’t know what to do with an exception you can just let it
out for something further out to deal with. Note that of course code too
far out won’t know enough to do something with it. But it avoids a
complex chain of if-statements at every call level to handle special
values.

Cheers,
Cameron Simpson cs@cskk.id.au

If I received TypeError message, why it is not triggering the “except TypeError:”?

Note the location of the error. It’s not happening in your try block. It’s happening before the call. Since test() takes 3 arguments, when you called it with only 1, python didn’t execute the function. There’s nothing you can do inside the function as currently written to deal with it. You could change the argument signature and parse the *args / **kwargs yourself, but that’s a lot more complex.

Outside the function you could put the test() call inside another try/except to catch this particular error.