Python Forum
How to programmatically exit without Traceback?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to programmatically exit without Traceback?
#5
I'm not sure how to use try/except, as Lars60+ suggested. The documentation says sys.exit raises a SystemExit exception and then exits. I want the exit but no further output. I don't understand why the first code exits without any coincident output, which is exactly what I want.

It seems to have worked seamlessly for you deanhystad. bowlofred also asked about the IDE. I'm running Python 3.7 in Spyder 4.2.0. Maybe it's specific to Spyder?

Thanks for the note about the style guidelines. What violated the style guidelines: using a colon to join two lines?

(Feb-24-2021, 05:20 PM)deanhystad Wrote: To exit a program without an error traceback you need to write a program that doesn't raise an exception. Or if it does raise an exception, provide an exception handler (as per Larz60+).

When I run your program like this:
import sys

def sum_two(a,b):
    if type(a) != int or type(b) != int:
        print('a and b must both be integers.')
        sys.exit()
    if a+b > 15 and a+b < 20:
        sum = 20
    else: sum = a+b
    return sum

sum_two(3.5, 6)
I do not get any mention of using exit or quit. I get this:
Output:
a and b must both be integers.
...
My guess is there is something catching the SystemExit exception and generating the messages you are seeing. What are you using for development? I am running Python 3.8.2 on Windows 10. I would stop using sys.exit() (I never use it) and find a better way to handle the problem.

By the way, ou can write your comparison:
if a+b > 15 and a+b < 20:
    sum = 20
like this:
if 15 < a+b < 20:
    sum = 20
And though this is allowed, all style guidelines say you should never use it;
else: sum = a+b
Reply


Messages In This Thread
RE: How to programmatically exit without Traceback? - by Mark17 - Feb-24-2021, 05:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question How create programmatically variables ? SpongeB0B 6 1,676 Aug-19-2023, 05:10 AM
Last Post: SpongeB0B
  How to programmatically stop a program in Jupyter Notebook? Mark17 11 39,157 Feb-12-2023, 01:41 PM
Last Post: jp21in
  Create Excel Line Chart Programmatically dee 3 1,320 Dec-30-2022, 08:44 PM
Last Post: dee
  python difference between sys.exit and exit() mg24 1 2,043 Nov-12-2022, 01:37 PM
Last Post: deanhystad
  Adding Language metadata to a PDF programmatically bhargavi22 0 2,014 Aug-17-2020, 12:53 PM
Last Post: bhargavi22

Forum Jump:

User Panel Messages

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