0

Is there a function in python 3 that is an equivalent to PHP's set_error_handler function? Is it good idea to use this equivalent in python scripting or there is better solution?

What I like about set_error_handler is that I can assign to it a function that for example logs and/or emails me when something goes wrong. I can then use trigger_error in my code and error_handler function will report back to me what and where did happen. any ideas how to replace it in python?

5 Answers 5

1

I think you are searching for sys.excepthook. It's also used by cgitb module.

Or maybe you can try library diagnostics that I wrote. It doesn't support e-mailing yet but feel free to open an issue with your proposal for it or send a pull request :)

Sign up to request clarification or add additional context in comments.

Comments

0

AFAIK Python uses mainly exceptions to handle errors and thus you should try to catch them instead of using a construct like set_error_handler. Do something like this:

try:
    code that might fail...
except:
    handle errors...

3 Comments

yeah. I agree that exceptions are the better way. I edited question to be more precise. could you help me now?
You just move the try/except block to the uppermost level in your code, then when an error occurs the code except will always be run when there is an exception, and the code you have in except could email you or whatever. To trigger this code you can use throw in the sub-functions that run inside the try block to throw exceptions on purpose.
I thought that I could use logging system and configure my loggers to send email and log to a file. then I can catch exceptions wherever they are. what do you thing about this approach?
0

See warnings for the python library used for handling warnings, which also links to captureWarnings in the logging library.

Most other issues are however thrown as exceptions, which you can try and catch as you see fit.

1 Comment

yeah. I agree that exceptions are the better way. I edited question to be more precise. could you help me now?
0

Use the Python exception handling mechanism (try/except) instead. It is much more flexible and fine-grained; you can catch errors arising from just one line of code (e.g. an IOError because a file was not found), or errors from your entire program (like set_error_handler does).

For CGI use, you can also use the cgitb module, which will print out very detailed error messages for debugging use:

import cgitb
cgitb.enable()

1 Comment

yeah. I agree that exceptions are the better way. I edited question to be more precise. could you help me now?
0

import cgitb() cgitb.enable() For CGI use, you can also use the cgitb module, which will print out very detailed error messages for debugging use:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.