Apart from a bare except: clause (which as others have said you shouldn't use), you can simply catch Exception:

import traceback
import logging

try:
    whatever()
except Exception as e:
    logging.error(traceback.format_exc())
    # Logs the error appropriately. 

You would normally only ever consider doing this at the outermost level of your code if for example you wanted to handle any otherwise uncaught exceptions before terminating.

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.

Answer from Duncan on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ catch "any" exception?
r/learnpython on Reddit: Catch "any" exception?
July 1, 2024 -

I'm writing a script that will interract with a website. Most of the handling of the website itself is in 1 function. Websites can of course change at any time and something as simple as the XPath of some element slightly changing can cause an exception to be thrown.

I'm wondering if there's a way to have a catch-all, like "if any expection is thrown in this function, execute this instead." For what i'm doing, i don't want 1 excpetion or unexpected behavior to terminate my entire script. I'd just want to log what happened, and continue on. Using a try except block for every instance of interracting with web elements would be tedious and quite frankly ugly coding and become annoying to maintain. Is there any way to basically have a default exception caught function?

Edit: if relevant, i'm using selenium to interract with websites. I'm also using multithreading to have multiple tabs open, so i don't want 1 tab having an issue causing all the other 10 to close out

Discussions

Handling exceptions in Python like a pro
try: ... except Exception as e: Don't catch Exception at a low level. Catch OSError, which now includes EnvironmentError and IOError. This should cover all the things that can occur for external reasons. If you get some unexpected error such as a SyntaxError or TypeError, for example, that ... More on news.ycombinator.com
๐ŸŒ news.ycombinator.com
25
46
May 20, 2021
Should I catch all exceptions?

Only catch when you have something meaningful to do in response.* Do not return instances of Exception; that makes the caller have to check whether the returned value was an Exception or not, which isn't how it's intended to be used, and denies the caller the option of passing on an exception to something that can handle it reasonably.

Sometimes, "translating" an exception into a different exception type makes sense.

* On rare occasions, "nothing" can be a meaningful action: the semantics are that you don't care that something went wrong and the best way to handle it is to proceed as if you'd never tried in the first place, and you can be sure that trying hasn't changed any global state that needs to be restored. My best example of this is if you're making a game and you try to play a sound, and the sound-handling code raises an exception because that specific audio file couldn't be opened - what else are you going to do? It's probably silly to tear the whole game down and say "sorry you can't play because this one sound doesn't work right", and there's probably nothing you can replace the sound with. Maybe you can make a note or something to not try the same sound again in the future; but OTOH, maybe the problem was temporary. Just because you opened the file doesn't mean it's been deleted, and even then, maybe it will be restored. Of course, that's different again if you have pre-packaged resources somehow; but I mean you can go back and forth on this kind of thing forever.

More on reddit.com
๐ŸŒ r/learnpython
8
11
December 15, 2015
How to catch all exceptions of one module (pymongo)
It's actually in the page you linked . exception pymongo.errors.PyMongoError Base class for all PyMongo exceptions. So you can just catch PyMongoError. More on reddit.com
๐ŸŒ r/learnpython
5
3
September 10, 2015
Exceptions & Logging
It entirely depends on the behaviour you want, which in turn depends on what kind of application this is and how serious the error is. If this is a command-line script for example, and this is a crucial part of the flow, then there's no point logging and continuing because the script can't work without this step. You should probably just let the error be raised. But for example if this is a web app, and this is just one of the operations it can do but it needs to continue to run even if this fails, then yes by all mean catch and log - but don't forget to also return an error message to the user. More on reddit.com
๐ŸŒ r/learnpython
9
5
November 13, 2024
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-catch-all-exceptions
Python - Catch All Exceptions - GeeksforGeeks
July 23, 2025 - In this article, we will discuss how to catch all exceptions in Python using try, except statements with the help of proper examples.
๐ŸŒ
Real Python
realpython.com โ€บ python-exceptions
Python Exceptions: An Introduction โ€“ Real Python
March 18, 2026 - Exceptions in Python occur when syntactically correct code results in an error. The try โ€ฆ except block lets you execute code and handle exceptions that arise. You can use the else, and finally keywords for more refined exception handling. Itโ€™s bad practice to catch all exceptions at once using except Exception or the bare except clause.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ errors.html
8. Errors and Exceptions โ€” Python 3.14.7 documentation
They include SystemExit which is raised by sys.exit() and KeyboardInterrupt which is raised when a user wishes to interrupt the program. Exception can be used as a wildcard that catches (almost) everything.
๐ŸŒ
Imperial College London
python.pages.doc.ic.ac.uk โ€บ lessons โ€บ core07 โ€บ 07-exception โ€บ 04-catchall.html
Lesson 7: Objects and Dictionaries > Catch all exceptions | Python Programming | Department of Computing | Imperial College London
โ€œA bare except: clause will catch SystemExit and KeyboardInterrupt exceptions, making it harder to interrupt a program with Control-C, and can disguise other problems. If you want to catch all exceptions that signal program errors, use except Exception: (bare except is equivalent to except ...
Find elsewhere
๐ŸŒ
Stackify
stackify.com โ€บ how-to-catch-all-exceptions-in-python
How to catch all exceptions in Python - Stackify
May 1, 2023 - Another way to catch all Python exceptions when it occurs during runtime is to use the raise keyword.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_try_except.asp
Python Try Except
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... The try block lets you test a block of code for errors. The except block lets you handle the error.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ builtins โ€บ exceptions.html
Built-in Exceptions โ€” Python 3.14.7 documentation
This design is so that except Exception catches an ExceptionGroup but not BaseExceptionGroup. The BaseExceptionGroup constructor returns an ExceptionGroup rather than a BaseExceptionGroup if all contained exceptions are Exception instances, so it can be used to make the selection automatic.
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ catching-all-exceptions
Catching all exceptions - Python Morsels
June 2, 2022 - Normally you can hit Ctrl+C to exit a running Python program. But Ctrl+C raises a KeyboardInterrupt exception, and we're catching that KeyboardInterrupt exception, because our bare except clause catches all exceptions:
๐ŸŒ
Quora
quora.com โ€บ How-can-you-handle-exceptions-in-Python-API-development
How to handle exceptions in Python API development - Quora
Here are some techniques for handling exceptions in Python API development: ... - Surround the code that might raise an exception with a `try` block, and catch the specific exceptions you expect in an `except` block. This helps you handle different types of exceptions differently. ... - The `finally` block allows ...
๐ŸŒ
Miguel Grinberg
blog.miguelgrinberg.com โ€บ post โ€บ the-ultimate-guide-to-error-handling-in-python
The Ultimate Guide to Error Handling in Python - miguelgrinberg.com
On the other side, we need to know what exceptions to write down in the except clause, because any exception classes that we miss are going to bubble up and potentially cause the Python application to crash. For a file deletion it is safe to assume that any errors that are raised are going to be OSError or one of its subclasses, but in other cases knowing what exceptions a function could raise requires looking at documentation or source code. You may ask why not catch all possible exceptions to make sure none are missed.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_tryexcept_block.htm
Python - The try-except Block
An exception is an error that occurs during the execution of a program, and handling these exceptions ensures your program can respond to unexpected situations. The try-except block in Python is used to catch ...
๐ŸŒ
Hacker News
news.ycombinator.com โ€บ item
Handling exceptions in Python like a pro | Hacker News
May 20, 2021 - try: ... except Exception as e: Don't catch Exception at a low level. Catch OSError, which now includes EnvironmentError and IOError. This should cover all the things that can occur for external reasons. If you get some unexpected error such as a SyntaxError or TypeError, for example, that ...
๐ŸŒ
Rollbar
rollbar.com โ€บ home โ€บ throwing exceptions in python
How to Throw Exceptions in Python | Rollbar
Now that you understand how to throw exceptions in Python manually, itโ€™s time to see how to handle those exceptions. Most modern programming languages use a construct called โ€œtry-catchโ€ for exception handling.
Published: May 22, 2026
๐ŸŒ
Honeybadger
honeybadger.io โ€บ blog โ€บ a-guide-to-exception-handling-in-python
The ultimate guide to Python exception handling - Honeybadger Developer Blog
March 28, 2025 - You can catch multiple exceptions with identical handling logic. Python lets you group them in a single except clause using parentheses.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ exception-handling-python
Exception & Error Handling in Python | Tutorial by DataCamp | DataCamp
May 29, 2026 - While it's possible to catch all exceptions using a bare except: clause, it's generally not recommended because it can catch unexpected exceptions and make debugging difficult. It's better to catch specific exceptions or use except Exception: to avoid catching system-exiting exceptions like ...
๐ŸŒ
SitePoint
sitepoint.com โ€บ blog โ€บ programming โ€บ a guide to python exception handling
A Guide to Python Exception Handling โ€” SitePoint
November 11, 2024 - We can have a generic except block to catch all exceptions in Python. The generic except block can be used alongside other specific except blocks in our program to catch unhandled exceptions.
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ python-exception-handling
Exception Handling Python: Try-Except Mastery - AskPython
January 25, 2026 - This approach makes error sources obvious. Each try block contains exactly the operation that might fail. Avoid catching Exception without re-raising it, as this swallows all errors including KeyboardInterrupt and SystemExit.