0

Generally, try/except clauses are used to handle errors and exceptions, catch them and act accordingly. But suppose, there is a script and the user is not sure where the exceptions can be raised. Now instead of applying try/except everywhere, is there any module or decorator which can catch any exception to occur and simply let the program not break?

Edit : This can not be a duplicate of try/except question. Of course I am aware of it and can apply it to all the possible places I know the exception may occur. I'm specifically looking for some module or a decorator which deals with exception handling. A builtin function which is called just before an error is raised would solve the problem too.

Himanshu Mishra
  • 8,510
  • 12
  • 37
  • 74
  • 3
    let the program not break? but what will it do instead of "not breaking". What you're asking exists, just can't put a name on it for now. – Jean-François Fabre Jan 14 '17 at 09:34
  • @Jean-FrançoisFabre Suppose there is a loop running to do some task over a bunch of lists. Now one of the times a list element is of unexpected type which the user is not aware of. But the main priority is to skip it, do not quit the program and proceed with the rest of the list elements. – Himanshu Mishra Jan 14 '17 at 09:41
  • In the duplication suggestion, there's an answer talking about `suppress` context manager. I think that's what you need. `from contextlib import suppress` – Jean-François Fabre Jan 14 '17 at 09:41
  • In the accepted answer to [How not to stop the execution of other function in python in case of Exception/Error](http://stackoverflow.com/questions/40102786/how-not-to-stop-the-execution-of-other-function-in-python-in-case-of-exception-e), example to create custom decorator to suppress error is mentioned – Moinuddin Quadri Jan 14 '17 at 10:06
  • 1
    This kind of thing is ok during development / debugging, but you shouldn't be doing it in production code. A program that raises random exceptions in random places is not to be trusted. Of course, it's perfectly fine for a program to raise expected exceptions, after all, that's a core feature of EAFP design, but they should be handled explicitly. – PM 2Ring Jan 14 '17 at 10:16
  • for the warnings that don't offer traces, you can turn those into bona fide errors (see https://stackoverflow.com/questions/24513013/traceback-from-a-warning) – 3pitt Jan 18 '18 at 20:47

1 Answers1

2

The proper way to handle exceptions is to use a try/except block surrounding the piece of code in which the exception could be raised. Sometimes this means wrapping your main() function (or whatever function runs the entire program) in a try/except block. That's fine. If you want to ignore the exception entirely, you can use contextlib.suppress, which is also fine and saves you a couple lines of code.

Now, if for some reason you need to react to uncaught exceptions and you are not able to surround the relevant piece of code with try/except, you can set sys.excepthook to a function that accepts the exception info as arguments and implements whatever handling you want, e.g. logging the exception. This is a fairly crude mechanism and I wouldn't recommend using it under normal circumstances, though. (And as far as I know, it cannot prevent the exception from being raised, if that's what you're asking - it can only give you a chance to run some of your own code before the stack unwinds and the program ends.)

David Z
  • 128,184
  • 27
  • 255
  • 279