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?
#4
I would recommend to omit the 'else'.

try:
    result = x / y
    print("result is", result)
except ZeroDivisionError:
    print("division by zero!")
If you predefine a 'result', you can also do:
result = None
x = 4
y = 0

try:
    result = x / y
except ZeroDivisionError:
    print("division by zero!")
print("result is", result)
If you do:

x = 4
y = 0
try:
    print("x is ", x)
    print("y is", y)
    result = x / y
    print("result is", result)
except ZeroDivisionError:
    print("division by zero!")
then you will see that the both first printed lines appear. During execution, everything is still fine there. Python only 'leaps' to the except part for further execution if it finds something going wrong. Everyting that can be done without the 'output' of the try-statement will be done when execution reaches is.


Oh, one thing: you are sure you use python 3? Your print-statements look like being python 2?
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 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,928 Nov-12-2022, 01:37 PM
Last Post: deanhystad
  name of exception for exit() Skaperen 2 2,452 May-24-2019, 07:07 PM
Last Post: Skaperen
  During handling of the above exception, another exception occurred Skaperen 7 27,037 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