The accepted answer is incorrect incomplete (at least for Python 3.6 and above).

By catching Exception you catch most errors - basically all the errors that any module you use might throw.

By catching BaseException, in addition to all the above exceptions, you also catch exceptions of the types SystemExit, KeyboardInterrupt, and GeneratorExit.

By catching KeyboardInterrupt, for example, you may stop your code from exiting after an initiated exit by the user (like pressing ^C in the console, or stopping launched application on some interpreters). This could be a wanted behavior (for example - to log an exit), but should be used with extreme care!

In the above example, by catching BaseException, you may cause your application to hang when you want it to exit.

Answer from EZLearner on Stack Overflow
🌐
Python
docs.python.org › 3 › builtins › exceptions.html
Built-in Exceptions — Python 3.14.7 documentation
In Python, all exceptions must be instances of a class that derives from BaseException. In a try statement with an except clause that mentions a particular class, that clause also handles any exception classes derived from that class (but not exception classes from which it is derived).
🌐
GeeksforGeeks
geeksforgeeks.org › python › built-exceptions-python
Python Built-in Exceptions - GeeksforGeeks
The program catches it and prints the error message, preventing a crash. ZeroDivisionError occurs when you attempt to divide a number by zero. Since division by zero is undefined in mathematics, Python raises this exception to signal the error.
Published: April 18, 2026
🌐
Real Python
realpython.com › ref › builtin-exceptions › baseexception
BaseException | Python’s Built-in Exceptions – Real Python
In Python, BaseException is a built-in exception that serves as the base class for all exceptions.
🌐
CodeQL
codeql.github.com › codeql-query-help › python › py-catch-base-exception
Except block handles ‘BaseException’ — CodeQL query help documentation
def call_main_program_implicit_handle_base_exception(): try: #application.main calls sys.exit() when done. application.main() except Exception as ex: log(ex) except: pass def call_main_program_explicit_handle_base_exception(): try: #application.main calls sys.exit() when done. application.main() except Exception as ex: log(ex) except BaseException: pass def call_main_program_fixed(): try: #application.main calls sys.exit() when done. application.main() except Exception as ex: log(ex) except SystemExit: pass · Python Language Reference: The try statement, Exceptions.
Top answer
1 of 2
50

The accepted answer is incorrect incomplete (at least for Python 3.6 and above).

By catching Exception you catch most errors - basically all the errors that any module you use might throw.

By catching BaseException, in addition to all the above exceptions, you also catch exceptions of the types SystemExit, KeyboardInterrupt, and GeneratorExit.

By catching KeyboardInterrupt, for example, you may stop your code from exiting after an initiated exit by the user (like pressing ^C in the console, or stopping launched application on some interpreters). This could be a wanted behavior (for example - to log an exit), but should be used with extreme care!

In the above example, by catching BaseException, you may cause your application to hang when you want it to exit.

2 of 2
39

Practically speaking, there is no difference between except: and except BaseException:, for any current Python release.

That's because you can't just raise any type of object as an exception. The raise statement explicitly disallows raising anything else:

[...] raise evaluates the first expression as the exception object. It must be either a subclass or an instance of BaseException.

Bold emphasis mine. This has not always been the case however, in older Python releases (2.4 and before) you could use strings as exceptions too.

The advantage then is that you get to have easy access to the caught exception. In order to be able to add as targetname, you must catch a specific class of exceptions, and only BaseException is going to do that.

You can still access the currently active exception by using sys.exc_info() though:

except:
    be = sys.exc_info()[1] 

Pick what you feel is more readable for your future self and for your colleagues.

🌐
Python
docs.python.org › 3.3 › library › exceptions.html
5. Built-in Exceptions — Python 3.3.7 documentation
In Python, all exceptions must be instances of a class that derives from BaseException. In a try statement with an except clause that mentions a particular class, that clause also handles any exception classes derived from that class (but not exception classes from which it is derived).
🌐
Python Tutorial
pythontutorial.net › home › python oop › python exceptions
Python Exceptions
March 28, 2025 - However, almost all built-in exception classes inherit from the Exception class, which is the subclass of the BaseException class: This page shows a complete class hierarchy for built-in exceptions in Python. The following example defines a list of three elements and attempts to access the ...
🌐
Python
docs.python.org › 3 › library › exceptions.html
Built-in Exceptions — Python 3.14.6 documentation
In Python, all exceptions must be instances of a class that derives from BaseException. In a try statement with an except clause that mentions a particular class, that clause also handles any exception classes derived from that class (but not exception classes from which it is derived).
Find elsewhere
🌐
Python documentation
docs.python.org › 3 › tutorial › errors.html
8. Errors and Exceptions — Python 3.14.7 documentation
For example: >>> raise NameError('HiThere') Traceback (most recent call last): File "<stdin>", line 1, in <module> raise NameError('HiThere') NameError: HiThere · The sole argument to raise indicates the exception to be raised. This must be either an exception instance or an exception class (a class that derives from BaseException...
🌐
TutorialsPoint
tutorialspoint.com › python-exception-base-classes
Exception and Exception Classes
You can create a custom exception class by Extending BaseException class or subclass of BaseException. From above diagram we can see most of the exception classes in Python extends from the BaseException class.
🌐
Medium
medium.com › data-bistrot › handling-exceptions-in-python-oop-e502b7e650dc
Handling Exceptions in Python OOP | by Gianpiero Andrenacci | AI Bistrot | Medium
May 7, 2024 - Examples of exceptions inheriting directly fromBaseException: -KeyboardInterrupt: Raised when the user interrupts the program (e.g., by pressing Ctrl+C) - SystemExit: Raised when the program exits normally - GeneratorExit: Raised when a generator ...
🌐
Stack Overflow
stackoverflow.com › questions › 42880527 › how-does-the-python-exception-or-base-exception-classes-work
How does the python exception or base exception classes work? - Stack Overflow
So I don't know if this is good practice, it probably is not but... I have a series of custom errors/warnings under a general error class which hands general stuff for them. class Error(Exception...
🌐
Medium
martinxpn.medium.com › exception-hierarchy-python-58-100-days-of-python-9d8585e6569b
Exception Hierarchy Python (58/100 Days of Python) | by Martin Mirakyan | Medium
April 10, 2023 - This means that the FileNotFoundError is a subclass of an OSError, which is a subclass of the Exception class, which itself inherits from the BaseException. To handle multiple exceptions with a single except statement, you can specify a tuple of exception types to catch, like this: try: # some code that may raise an exception except (ExceptionType1, ExceptionType2): # handle the exception · In this example, both ExceptionType1 and ExceptionType2 are exceptions that you want to catch.
🌐
Real Python
realpython.com › python-built-in-exceptions
Python's Built-in Exceptions: A Walkthrough With Examples – Real Python
March 18, 2026 - In Python 3.11 and greater, you’ll have the ExceptionGroup and BaseExceptionGroup classes. You can use them when you need to raise multiple unrelated exceptions at the same time.
Top answer
1 of 8
160

Solution - almost no coding needed

Just inherit your exception class from Exception and pass the message as the first parameter to the constructor

Example:

class MyException(Exception):
    """My documentation"""

try:
    raise MyException('my detailed description')
except MyException as my:
    print my # outputs 'my detailed description'

You can use str(my) or (less elegant) my.args[0] to access the custom message.

Background

In the newer versions of Python (from 2.6) we are supposed to inherit our custom exception classes from Exception which (starting from Python 2.5) inherits from BaseException. The background is described in detail in PEP 352.

class BaseException(object):

    """Superclass representing the base of the exception hierarchy.
    Provides an 'args' attribute that contains all arguments passed
    to the constructor.  Suggested practice, though, is that only a
    single string argument be passed to the constructor."""

__str__ and __repr__ are already implemented in a meaningful way, especially for the case of only one arg (that can be used as message).

You do not need to repeat __str__ or __init__ implementation or create _get_message as suggested by others.

2 of 8
26

Yes, it's deprecated in Python 2.6 because it's going away in Python 3.0

BaseException class does not provide a way to store error message anymore. You'll have to implement it yourself. You can do this with a subclass that uses a property for storing the message.

class MyException(Exception):
    def _get_message(self): 
        return self._message
    def _set_message(self, message): 
        self._message = message
    message = property(_get_message, _set_message)

Hope this helps

🌐
Python Module of the Week
pymotw.com › 2 › exceptions
exceptions – Built-in error classes - Python Module of the Week
$ python exceptions_KeyError.py Traceback (most recent call last): File "exceptions_KeyError.py", line 13, in <module> print d['c'] KeyError: 'c' A KeyboardInterrupt occurs whenever the user presses Ctrl-C (or Delete) to stop a running program. Unlike most of the other exceptions, KeyboardInterrupt inherits directly from BaseException to avoid being caught by global exception handlers that catch Exception.
🌐
Runebook.dev
runebook.dev › en › docs › python › library › exceptions › BaseException
Understanding BaseException: The Root of Python's Error Hierarchy
If a user tries to exit the program with Ctrl+C, catching KeyboardInterrupt with BaseException might stop the exit signal, making your program hard to terminate. It hides bugs. Catching everything makes it impossible to distinguish between an expected recoverable error (like a KeyError in a dictionary lookup) and a serious, unexpected programming error. The Python standard practice is to catch the subclass Exception instead.
🌐
Medium
gokulapriyan.medium.com › the-pythonic-path-exploring-the-exception-hierarchy-in-python-04759e0b0219
“The Pythonic Path: Exploring the Exception Hierarchy in Python” | by Gokulapriyan | Medium
August 28, 2024 - BaseException Example try: raise BaseException("BaseException raised") except BaseException as e: print(f"Caught: {e}") SystemExit Example import sys try: sys.exit("Exiting program") except SystemExit as e: print(f"Caught: {e}") KeyboardInterrupt ...
🌐
Python
docs.python.org › 3.15 › library › exceptions.html
Built-in Exceptions — Python 3.15.0rc1 documentation
In Python, all exceptions must be instances of a class that derives from BaseException. In a try statement with an except clause that mentions a particular class, that clause also handles any exception classes derived from that class (but not exception classes from which it is derived).
🌐
Team Treehouse
teamtreehouse.com › community › what-is-a-baseexception
What is a BaseException? (Example) | Treehouse Community
June 8, 2018 - BaseException is a class which all other exception types inherit from, it can be used to create custom exception types as well, but that is a more complex topic which you likely have not gotten to yet. If you change your raise statement to something like this: raise ValueError("There are only {} tickets remaining".format(tickets_remaining)) Then that error should go away. Python Web Development Techdegree Student 2,324 Points ·