find the error code given by python and then put it in the except case

i want know how do it ? is it possible write it to file and i look at it later ?
Mehan COIT ADMINISTRATORAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun VermaakTechnical SpecialistCommented:
You can use
except Exception as e: print(e)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gelonidaCommented:
yoy can reassign sys.excepthook to a function, that will be called foer any uncaught exception:

#!/usr/bin/env python
import sys

def func_that_fails(a, b):
    print("now I'll try to divide %d by %d" % (a,b))
    return a/b

def somedivisions():
    for a in range(10):
        b = 5 -a
        print("%d / %d = %f" % (a, b, func_that_fails(a, b)))

def my_exception_handler(exctype, value, tb):
    import traceback
    error_message = ''.join(traceback.format_tb(tb))
    error_message += "\n%s: %s\n" % (exctype, value)
    # comment this line if you want the error just in the file
    sys.stderr.write(error_message) 
    with open("myerror.log", "w") as fout:
        fout.write(error_message)

sys.excepthook =my_exception_handler

if __name__ == "__main__":
    somedivisions()

Open in new window


You can also explicitely try: / except:  the main function call.
Or you can (if you use it already) use python's logging module in combination with sys.excepthook to log the exceptions into your already existing log file.
gelonidaCommented:
Some more comments:

Above solution will always log an error and an exception if you stop your program with ctrl-C or if you kill it.
If that's an issue, then you can specifically exclude KeyboardInterrupt and SystemExit

Above solution has also solutions with multithreaded applications.
Exceptions in a thread will unfortunately not be caught by sys.excepthook

However there are some potential tricks / hacks, which might help you in such cases
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.