Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Home made exception

You can create your own exception classes that will allow the user to know what kind of an exception was caught or to capture only the exceptions of that type.

class MyException(Exception):
    pass

def some():
    raise MyException("Some Error")

def main():
    try:
        some()
    except Exception as err:
        print(err)
        print("Type: " + type(err).__name__)

    try:
        some()
    except MyException as err:
        print(err)

main()

Output:

Some Error
Type: MyException
Some Error