Python Forum

Full Version: choice of exception type
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
in some new script code i want to abort, raising an exception, if found to be running under Python2 and unable to re-run the script under Python3. what exception type should i use for the case of being under Python2 with no way to switch to Python3? i don't really want to use OSError.

if bytes == str:
    import os,sys
    for p in os.environ.get('PATH','').split(':'):
        if p and os.path.exists(p+'/python3'):
            try:
                os.execvp(p+'/python3',['python3']+sys.argv)
            except:
                pass
    raise VersionError('Python2 is no longer supported - Please install Python3')
I think you would normally raise a subtype of RuntimeError. The python 2 documentation says
Quote:exception RuntimeError
Raised when an error is detected that doesn’t fall in any of the other categories. The associated value is a string indicating what precisely went wrong.
So you could write
class Python3UnavailableError(RuntimeError):
    pass