Exception handling print statements not working, but error types are?

Viewed 26

My code is below. The exception handling itself works, but I am unable to get the error messages specified to print.

import numpy as np
def compute_circle_area(r):
    try:
        if type(r) == 'complex' :
          raise(TypeError('Argument is complex'))
        elif r < 0 :
          raise(ValueError('Argument < 0'))
        elif type(r) == str :
          raise(TypeError('Argument is wrong type'))
  
        a = np.pi * r * r

    except ValueError as ve:
      print("Value Error", ve)
    except TypeError as te:
      print("Type Error", te)
    else:
      print('Argument works, solution is: ', a)
    finally:
      print("Let's continue")

print(compute_circle_area(2.1))
print(compute_circle_area(2+1j))
print(compute_circle_area(-2.1))
print(compute_circle_area('radius'))
1 Answers

If you want to print just the error message, you have to print the args attribute, not the error. Else, it will give you the whole representation of the error.

Related