Python Forum
How to exit after a try exception?
Thread Rating:
  • 3 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to exit after a try exception?
#6
(Mar-04-2017, 01:47 AM)ichabod801 Wrote: I'm not sure what you mean by "exit the program." In one sense, if you want to exit the program at that point, you don't put in a try/except block. The ZeroDivisionError will exit the program. If this was part of a function or method, you could put a return statement there to exit the function/method. You could use sys.exit(), but that just raises a different exception, so it seems kind of pointless.

If you want to continue with the program, you just don't do anything. Your example will continue execution after the else block. I'm not sure what you mean by "continue on a different path." If you want completely different execution of the entire program after the ZeroDivision error, one path needs to be in the except block and the other in the else block. I mean, you could call different functions from those places, so the code is elsewhere, but the whole rest of the program would have to be in those two blocks.

Hi ichabod801,

My intent is to compile my program to a .exe using cx_freeze or py2exe.  I will be using Tkinter to create a GUI for the user.  If Python gets a "file not found" or other error, I want to display a message to let the user know what happened.  Then when the user clicks OK, exit my program.  I didn't know how to exit the code in "except:", after I displayed the error message, to terminate my program gracefully without the traceback causing an exit error or if it would even be visible to the user in a .exe program.  (I'm a total newbie writing my first Python program, though I've programmed in Basic, C and assembler about 20 years ago.)

Alternatively, I wanted to give the user a way to go back to the start of my program and redo his inputs without the exception error terminating and exiting my program.  I want to learn how to do that too.

The divide by zero was just an example I happened to copy from the web.  I see now that it was in Python 2 instead of Python 3.  I'm using Python 3.6.

EDIT:  "The ZeroDivisionError will exit the program."
BTW, I just ran my program again.  After the "except:" error, the code prints "Image not found" in the console.  Then the program just continues and displays the user GUI anyway.  It does not exit the program. 

try:
    imageEx = PhotoImage(file = 'Lisa.gif')
    Label(leftFrame, image=imageEx).grid(row=2, column=0, padx=10, pady=2)
except:
    print("Image not found")
Thanks for the help,
Raptor88

(Mar-04-2017, 04:14 AM)wavic Wrote: Hello!

The sys module has what you want.

 
import sys

sys.exit()
sys.exit("error message here for instance") # if something goes wrong

Hi wavic,

Thanks for posting the code.  It's very helpful to learn how to exit a Python program. 

I had looked for "exit" in the index of the Python books I have and it's not even listed.  One book does have "SytemExit" in the index but how would a newbie know the keywords to search for?  Python book authors can spend so much time writing their books but so little time in making their index capable for newbies who don't know the proper keywords.

Thanks again,
Raptor88

(Mar-04-2017, 08:44 AM)merlem Wrote: I would recommend to omit the 'else'.
.... snip ....
Oh, one thing: you are sure you use python 3? Your print-statements look like being python 2?

Thank you for your input.  I am using Python 3.6 but inadvertently copied a sample code from Python 2.  Please see my response to Ichabod808 for what I'm trying to do.

(Mar-04-2017, 10:13 AM)snippsat Wrote: You a function,then return will exit out of function.
.... snip ....

Thank you for your input.  Please see my response to Ichabod808 for what I'm trying to do.
Reply


Messages In This Thread
How to exit after a try exception? - by Raptor88 - Mar-04-2017, 01:15 AM
RE: How to exit after a try exception? - by Raptor88 - Mar-04-2017, 07:41 PM
RE: How to exit after a try exception? - by wavic - Mar-04-2017, 04:14 AM
RE: How to exit after a try exception? - by merlem - Mar-04-2017, 08:44 AM
RE: How to exit after a try exception? - by wavic - Mar-04-2017, 09:20 PM
RE: How to exit after a try exception? - by wavic - Mar-05-2017, 08:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python difference between sys.exit and exit() mg24 1 1,894 Nov-12-2022, 01:37 PM
Last Post: deanhystad
  name of exception for exit() Skaperen 2 2,439 May-24-2019, 07:07 PM
Last Post: Skaperen
  During handling of the above exception, another exception occurred Skaperen 7 26,983 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