If I run this line of code, I get an error
from time import datetime
or if I run the following code also, I get an error
import exception
How do I catch such exceptions and know their types?
ImportError is raised when a module, or member of a module, cannot be imported. There are a two conditions where an ImportError might be raised.
import sys try: from exception import myexception except Exception as e: print e print sys.exc_type
OUTPUT
No module named exception <type 'exceptions.ImportError'>
import sys try: from time import datetime except Exception as e: print e print sys.exc_type
cannot import name datetime <type 'exceptions.ImportError'>
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more