Replace type() with try/except