62

Is it possible to catch any error in Python? I don't care what the specific exceptions will be, because all of them will have the same fallback.

5
  • 4
    And like the dog that chases cars down the country road: what will you do when you catch it? Some errors (i.e. MemoryError) mean Python is already crashing. Commented Jul 25, 2011 at 14:46
  • @S.Lott Well I've used that in some long running applications to inform me of the problem if possible. Sure some errors will be grave enough that nothing will help (because you can't do anything any longer), but still it's better than nothing. Commented Jul 25, 2011 at 15:13
  • @Voo: "better than nothing"? I find that it's usually worse than noting. Out of Memory, for example, means things have already failed to operate properly. Recovery of some valid "prior state" of the Python computation seems impossible. Commented Jul 25, 2011 at 18:18
  • 1
    Does this answer your question? About catching ANY exception Commented Oct 8, 2020 at 15:53
  • maybe this is a better answer? stackoverflow.com/a/4992124/1601580 I like this comment in particular The advantage of except Exception over the bare except is that there are a few exceptions that it wont catch, most obviously KeyboardInterrupt and SystemExit: if you caught and swallowed those then you could make it hard for anyone to exit your script. Commented Jul 22, 2022 at 17:54

8 Answers 8

75

Using except by itself will catch any exception short of a segfault.

try: something() except: fallback() 

You might want to handle KeyboardInterrupt separately in case you need to use it to exit your script:

try: something() except KeyboardInterrupt: return except: fallback() 

There's a nice list of basic exceptions you can catch here. I also quite like the traceback module for retrieving a call stack from the exception. Try traceback.format_exc() or traceback.print_exc() in an exception handler.

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

9 Comments

In most cases, you should also let NameError (and perhaps a few others like AttributeError) propagate as they inidicate bugs in your code.
It's often better practice to catch except Exception - KeyboardInterrupt and SystemExit don't inherit from Exception, so you can still 'break out'. Depends on what you're doing it for, of course.
Where does fallback() come from? I receive "NameError: name 'fallback' is not defined"!
but I want to pointer to e the exception NO MATTER THE TYPE.
maybe this is a better answer? stackoverflow.com/a/4992124/1601580 I like this comment in particular The advantage of except Exception over the bare except is that there are a few exceptions that it wont catch, most obviously KeyboardInterrupt and SystemExit: if you caught and swallowed those then you could make it hard for anyone to exit your script.
|
61
try: # do something except Exception, e: # handle it 

For Python 3.x:

try: # do something except Exception as e: # handle it 

8 Comments

how is this different from the accepted answer? I see the code is different but do you mind maybe adding some comments/details?
Exception will not catch all the errors,however except will.
I dont think this is proper syntax
@CharlieParker It's different because in this case you have access to an Exception object.
@sergzach so the first option captures ALL errors and gives me a pointer to the exception object through e?`
|
16

You might want also to look at sys.excepthook:

When an exception is raised and uncaught, the interpreter calls sys.excepthook with three arguments, the exception class, exception instance, and a traceback object. In an interactive session this happens just before control is returned to the prompt; in a Python program this happens just before the program exits. The handling of such top-level exceptions can be customized by assigning another three-argument function to sys.excepthook.

Example:

def except_hook(type, value, tback): # manage unhandled exception here sys.__excepthook__(type, value, tback) # then call the default handler sys.excepthook = except_hook 

1 Comment

but I want to pointer to e the exception NO MATTER THE TYPE.
15
+25

Quoting the bounty text:

I want to be able to capture ANY exception even weird ones like keyboard interrupt or even system exit (e.g. if my HPC manger throws an error) and get a handle to the exception object e, whatever it might be. I want to process e and custom print it or even send it by email

Look at the exception hierarchy, you need to catch BaseException:

BaseException +-- SystemExit +-- KeyboardInterrupt +-- GeneratorExit +-- Exception 

This will capture KeyboardInterrupt, SystemExit, and GeneratorExit, which all inherit from BaseException but not from Exception, e.g.

try: raise SystemExit except BaseException as e: print("hello world!") 

1 Comment

In my case, I had to do print(e.with_traceback(None)), because with_traceback expects one argument.
6

Not mentioning the type of exception you want to handle itself does the job.

Try this:

try: #code in which you expect an exception except: #prints the exception occured 

if you want to know the type of exception that occurred:

try: # code in which you expect an exception except Exception as e: print(e) # for any exception to be catched print(type(e)) # to know the type of exception. 

for detailed explanation go trough this https://www.tutorialspoint.com/python/python_exceptions.htm

1 Comment

... and with print(type(e)) you get also the type of the error (which can be useful too)
3
# in python 3 # if you want the error try: func() except Exception as e: exceptionFunc(e) # if you simply want to know an error occurs try: func() except: exceptionFunc() # if you don't even wanna do anything try: func() except: pass 

Comments

1

The following only worked for me (both in PY2 and PY3):

try: # (Anything that produces any kind of error) except: ertype = sys.exc_info()[0] # E.g. <class 'PermissionError'> description = sys.exc_info()[1] # E.g. [Errno 13] Permission denied: ... # (Handle as needed ) 

2 Comments

but I want to pointer to e the exception NO MATTER THE TYPE.
@CharlieParker You offered the bounty but never awarded it, did you solve your issue?
0

Built-In Exceptions in Python

Built-In exception classes are divided into Base error classes from which the error classes are defined and Concrete error classes which define exceptions which you are more likely to see time to time.

The more detailed document about the buit-In exception can be found in [https://docs.python.org/3/library/exceptions.html]

Custom Exceptions

It is used to fit your specific application situation. For example, you can create your own exception as RecipeNotValidError as the recipe is not valid in your class for developing a cooking app.

Implementation

 class RecipeNotValidError(Exception): def __init__(self): self.message = "Your recipe is not valid" try: raise RecipeNotValidError except RecipeNotValidError as e: print(e.message) 

These are custom exceptions that are not defined in the standard library. The steps you can follow to create custom classes are :

  1. Subclass the Exception class.
  2. Create a new Exception class of your choice.
  3. Write your code and use the try...except flow to capture and handle your custom exception.

1 Comment

Yes, but the question here was "how to catch any exception and get its error object?" It was about handling even unexpected errors.