1

I am [relatively] new to python, and I'm making an application that calculates compound interest. I want users to be able to report their crashes, and to do this I need to write some code that executes when there is a problem and the program stops running. I just want to display some text and show the contents of some variables so the user can cut and paste them into an email.

Is this feasible?

Thanks!

user3724472
  • 29
  • 1
  • 4

3 Answers3

0

Sure take a look at how to use try/except here: https://docs.python.org/3.4/tutorial/errors.html

Example:

#!/usr/bin/env python3
import sys

def calculation():
    return 1/0

if __name__ == "__main__":
    try:
        print(calculation())
    except Exception as e:
        print("Oops, something went wrong: {}".format(e), file=sys.stderr)
        sys.exit(1)
0

If you know something that will throw an error, use a try except finally block.

try:
    raise ANonSenseError()
except ANonSenseError:
    # Log it
    # Bring some dialog for users to report it.
finally:
    # Well, this part is optional, but you can use it in case an exception isn't thrown
    # It will always run

If you don't want to use the try except block, you could try and establish something that will have to execute if the program stops. You might not be able to find out what error it is, but you will have the flexibility of doing anything freely after the main section of your program crashes.

Also, a suggestion if you're using Tkinter: use Tkinter.Toplevel to raise a top level window.

Take a look at this question, which should give you some guidance about raising a new window when your main application crashes.

Community
  • 1
  • 1
Zizouz212
  • 4,908
  • 5
  • 42
  • 66
0

Use the traceback module to handle you stacktrace. I use this under most of my exceptions

import traceback
import sys, os
try:
    bla
except Exception as e:
    top = traceback.extract_tb(sys.exc_info()[2])[-1]
    print '{} : {} in {} at line {}'.format(type(e).__name__, str(e), os.path.basename(top[0]), str(top[1]))

Output:

NameError : name 'bla' is not defined in test.py at line 4
letsc
  • 2,515
  • 5
  • 35
  • 54