[Tutor] Exception not working as expected?

Chip Wachob wachobc at gmail.com
Fri Mar 1 14:23:50 EST 2019


Eryk

Thank you for the in-depth explanation and code sample.

I was able to use the sample code to create a functional script.

Now, I'm still not completely following what you mean in the last couple of
paragraphs about the except ValueError.

I realized that I am never actually trapping on the else statement in my
original code.  When I enter inappropriate values, the exception is always
trapping, not the else statement.  Which seems sort of odd to me.

I'm not sure what you mean by putting the except close to the int().

Generally, I feel like I'm getting a handle on this, but that actually
leads me to a related question.

As you can tell, this is just the top of a whole bunch of menu tree paths
that the user can take.  Below is a very rough textual representation of
the layout of the menus.  I understand how the sys.exit() works in the case
of the initial menu, but I don't follow how I can use that same approach to
navigate 'back' up the menu tree.  Each major Test (Type A, Type B, etc) is
a .py file unto itself.

Something about this is feeling like maybe it should be a class, or some
other function that I call throughout my program.

How can I best handle this situation and, how do I trap those invalid
entries and, as you say, "continue" the try statements?


Main Menu
    |
    |-- Test Type A Items
    |    |-- 1. Test A1
    |    |-- 2. Test A2
    |    |-- 3. Test A3
    |    |-- 0. Back to Main
    |
    |-- Test Type B Items
    |    |-- 1. Test B1
    |    |-- 2. Test B2
    |    |-- 3. Test B3
    |    |-- 0. Back to Main
    |
    |-- Test Individual Item
    |    |-- 1. Test I1
    |    |-- 2. Test I2
    |    |-- 3. Test I3
    |    |-- 0. Back to Main
    |
    |-- Quit

Thank you,


On Thu, Feb 28, 2019 at 9:31 PM eryk sun <eryksun at gmail.com> wrote:

> On 2/28/19, Chip Wachob <wachobc at gmail.com> wrote:
> >
> > Python 2.7 & Windows and also Linux are the platforms being used.
> Running
> > the code from the command line / terminal as   python except.py.  Note
> that
> > it does work properly in Linux.  So I'm guessing I need to test for a
> > different exception along with the KeyboardInterrupt??
>
> When reading from the console, we depend on a particular error being
> set in order to distinguish an interrupted read from end-of-file
> (EOF), but this error doesn't get set in Windows 8+ due to a bug in
> the Windows API. I created an issue with Microsoft's console team, but
> thus far they've ignored it.
>
> Due to this bug, Python 2 code that calls raw_input() or input() also
> needs to handle EOFError, which should probably be handled anyway.
> However, it's not enough to separately handle KeyboardInterrupt and
> EOFError, since the KeyboardInterrupt may get raised while handling
> EOFError. This can happen because Windows calls the console control
> handler asynchronously in a new thread. We need a bit of a kludge that
> checks for a delayed KeyboardInterrupt. For example, the following
> shows a case that uses a common handler for EOF and Ctrl+C:
>
>     import os
>     import sys
>     import time
>
>     def main():
>         try:
>             os.system('cls' if os.name == 'nt' else 'clear')
>             print "\n\n\n\n Welcome to Test.\n"
>             time.sleep(DISPLAY_TIME)
>             start_menu()
>         except (KeyboardInterrupt, EOFError):
>             try:
>                 time.sleep(0.1)
>             except KeyboardInterrupt:
>                 pass
>             # provides a clean exit
>             print "\n\n Keyboard Interrupt or EOF - Exiting \n\n"
>             time.sleep(5)
>         print "End of Script\n"
>         return 0
>
>     if __name__ == "__main__":
>         sys.exit(main())
>
> >     # trap no choice, just Enter key cases
> >     except ValueError:
> >         # handles non-entry selections like 'Enter'
> >         print "Invalid selection please try again"
> >         time.sleep(DISPLAY_TIME / 2)
> >         start_menu()
>
> ValueError should be handled near the int() conversion that raises it,
> and the exception handler should `continue` the loop instead of
> recursively calling start_menu().
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list