Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
choice of exception type
#1
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')
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  combobox_callback(choice choice part of openfile name (kind of dependency) janeik 9 1,449 Sep-10-2023, 10:27 PM
Last Post: janeik
  what type of exception to use? Skaperen 3 1,054 Sep-30-2022, 05:00 PM
Last Post: Skaperen
  random.choice HELP samuelbachorik 4 2,278 Aug-18-2021, 03:24 PM
Last Post: naughtyCat
  ???: if "{choice}" in self._command BaiYouLing4 3 2,035 Aug-23-2020, 05:40 AM
Last Post: BaiYouLing4
  Exception: Returned Type Mismatch Error devansing 1 5,165 Mar-06-2020, 07:26 PM
Last Post: ndc85430
  Type hinting - return type based on parameter micseydel 2 2,492 Jan-14-2020, 01:20 AM
Last Post: micseydel
  How to fix 'uncaught exception of type NSException' in Python MonsterPython 0 2,162 Jul-09-2019, 06:52 AM
Last Post: MonsterPython
  Stack trace shows different exception type than print micseydel 5 4,423 Apr-01-2019, 10:24 PM
Last Post: micseydel
  During handling of the above exception, another exception occurred Skaperen 7 26,902 Dec-21-2018, 10:58 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020