๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ errors.html
8. Errors and Exceptions โ€” Python 3.14.7 documentation
Built-in Exceptions lists the built-in exceptions and their meanings. It is possible to write programs that handle selected exceptions. Look at the following example, which asks the user for input until a valid integer has been entered, but allows the user to interrupt the program (using Control-C or whatever the operating system supports); note that a user-generated interruption is signalled by raising the KeyboardInterrupt exception.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-exception-handling
Python Exception Handling - GeeksforGeeks
We can choose from built-in exceptions or define our own custom exceptions by inheriting from Python's built-in Exception class. ... Example: This code raises a ValueError if an invalid age is given.
Published: May 29, 2026
Discussions

Learning (not) to Handle Exceptions

Overall this seems like a good introduction to exceptions in Python, but I have a couple criticisms: -You almost never want an 'except' block that doesn't specify an exception type. lt will catch literally anything, including a lot of things you don't want: system exit, keyboard interrupt, out of memory errors, and more. -You rarely want to catch 'Exception', except possibly in top level code, for similar reasons. You should only catch the errors you're prepared to handle, and let higher level code sort out anything else. -Exception handlers that just print a static message are close to useless for debugging. Exception handlers that only print the error message aren't much better. You generally want to print the error type, message, any debugging data included with the error, and stack trace. Some of the articles in the example print the first three, but the stack trace is arguably the most important one since you'll see exactly where the error happened and what the call stack looked like. -Minor complaint - instead of this: try: file = open(...) doStuff(file) finally: file.close() Do this: with open(...) as file: doStuff(file) Or if it was a contrived example for readers, at least mention the better way to do it. :)

More on reddit.com
๐ŸŒ r/Python
16
83
July 12, 2018
What are some good real-world examples of exceptions in Python? (like what open source projects are a good example of exception usage?)

Most medium and bigger sized projects use exceptions, as do small projects. Look up the code for Python modules that are really popular, like requests or flask, and you'll find exception handling as well as custom exceptions. I can give you some of my projects as well as other projects that use exceptions if you'd like, so here:

https://github.com/TabulateJarl8/academiic-public/blob/4663a91a751545291f1b0855203dd3db4a0703a8/app.py#L209

https://github.com/TabulateJarl8/ti842py/blob/84c427579477c3a01c195245a2aef63c3ba079ea/ti842py/main.py#L21

https://github.com/willmcgugan/rich/blob/c9afafdd680831a43956906d56c78d9933aaf232/rich/console.py#L938

https://github.com/stub42/pytz/blob/df59d3af429d46ee575ab5d3a7fe3f8cd49a74bc/src/pytz/tzinfo.py#L7

https://gitlab.com/TurboWafflz/ImaginaryInfinity-Calculator/-/blob/d5013bf567b4d2b6a3d7fde069b70adcb6aa36a1/system/systemPlugins/pm.py#L399

Custom exceptions: https://github.com/psf/requests/blob/1466ad713cf84738cd28f1224a7ab4a19e50e361/requests/exceptions.py

More on reddit.com
๐ŸŒ r/learnpython
3
0
July 5, 2021
Can anyone explain Exception Handling to me?
Prompt for input that's supposed to be an integer: item_number = int(input("Enter an item NUMBER: ")) If the value supplied is not a number ("A"), the Python interpreter throws a ValueError: ValueError: invalid literal for int() with base 10: ' A' Since this is an expected error the program might run into, you can catch it with a try..except: try: item_number = int(input("Enter an item NUMBER: ")) except ValueError: print("You were supposed to enter a number.") Printing a custom message is a simple example of how you can handle an error. Other options are re-running a function to keep prompting the user for valid input, move on to different parts of the program, etc. More on reddit.com
๐ŸŒ r/learnpython
13
12
September 24, 2023
Python - what is an exception and what does handling them mean?
So, there are 2 cases where an exception occurs: you write it input as the cause Right? More on reddit.com
๐ŸŒ r/learnprogramming
11
1
October 2, 2021
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ builtins โ€บ exceptions.html
Built-in Exceptions โ€” Python 3.14.7 documentation
Some built-in exceptions (like OSError) expect a certain number of arguments and assign a special meaning to the elements of this tuple, while others are usually called only with a single string giving an error message. ... This method sets tb as the new traceback for the exception and returns the exception object. It was more commonly used before the exception chaining features of PEP 3134 became available. The following example shows how we can convert an instance of SomeException into an instance of OtherException while preserving the traceback.
๐ŸŒ
Real Python
realpython.com โ€บ python-exceptions
Python Exceptions: An Introduction โ€“ Real Python
March 18, 2026 - In this example, there was one bracket too many. Remove it and run your code again: ... >>> print(0 / 0) Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError: division by zero ยท This time, you ran into an exception error. This type of error occurs whenever syntactically correct Python code results in an error.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ exception-handling-python
Exception & Error Handling in Python | Tutorial by DataCamp | DataCamp
May 29, 2026 - The most simple way of handling exceptions in Python is by using the try and except block. Run the code under the try statement. When an exception is raised, execute the code under the except statement. Instead of stopping at error or exception, our code will move on to alternative solutions. In the first example, we will try to print the undefined x variable.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ exceptions
Python Exceptions (With Examples)
Whenever these types of runtime errors occur, Python creates an exception object. If not handled properly, it prints a traceback to that error along with some details about why that error occurred. ... Traceback (most recent call last): File "<string>", line 1, in <module> ZeroDivisionError: division by zero
๐ŸŒ
Dataquest
dataquest.io โ€บ home โ€บ blog โ€บ python exceptions: the ultimate beginner's guide (with examples)
Python Exceptions: The Ultimate Beginner's Guide (with Examples)
March 6, 2023 - In the opposite case, if an exception is thrown, the execution of the try block is immediately stopped, and the program handles the raised exception by running the alternative code determined in the except block. After that, the Python script continues working and executes the rest of the code. Let's see how it works by the example of our initial small piece of code print(x), which raised earlier a NameError:
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ errors-and-exceptions-in-python
Errors and Exceptions in Python - GeeksforGeeks
May 29, 2026 - Python detects these errors before running the program and shows the location of the mistake. Example: In this example, the code gives a syntax error because the colon (:) is missing after the if statement.
Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_try_except.asp
Python Try Except
See more Error types in our Python Built-in Exceptions Reference. You can use the else keyword to define a block of code to be executed if no errors were raised: In this example, the try block does not generate any error:
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ exception-handling
Python Exception Handling (With Examples)
In Python, the finally block is always executed no matter whether there is an exception or not. The finally block is optional. And, for each try block, there can be only one finally block. ... try: numerator = 10 denominator = 0 result = numerator/denominator print(result) except: print("Error: Denominator cannot be 0.") finally: print("This is finally block.") ... Error: Denominator cannot be 0. This is finally block. In the above example, we are dividing a number by 0 inside the try block.
๐ŸŒ
Honeybadger
honeybadger.io โ€บ blog โ€บ a-guide-to-exception-handling-in-python
The ultimate guide to Python exception handling - Honeybadger Developer Blog
March 28, 2025 - Exceptions can occur for various ... conditions. Examples of exceptions in Python include ZeroDivisionError, TypeError, FileNotFoundError, and ValueError, among others....
๐ŸŒ
SitePoint
sitepoint.com โ€บ blog โ€บ programming โ€บ a guide to python exception handling
A Guide to Python Exception Handling โ€” SitePoint
November 11, 2024 - In the example above, we place the first print statement within the try block. The piece of code within this block will raise an exception, because dividing a number by zero has no meaning. The except block will catch the exception raised in the try block. The try and except blocks are often used together for handling exceptions in Python...
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_exceptions.htm
Python - Exceptions Handling
For example, to capture above exception, we must write the except clause as follows โˆ’ ยท try: Business Logic here... except "Invalid level!": Exception handling here... else: Rest of the code here... Python also allows you to create your own exceptions by deriving classes from the standard built-in exceptions.
๐ŸŒ
Tutorial Teacher
tutorialsteacher.com โ€บ python โ€บ exception-handling-in-python
Exception Handling in Python
The following example uses two ... ... try: a=5 b=0 print (a/b) except TypeError: print('Unsupported operation') except ZeroDivisionError: print ('Division by zero not allowed') except: print('Some error occurred.') print ('Out of try except blocks') ... However, ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_ref_exceptions.asp
Python Built-in Exceptions
The table below shows built-in exceptions that are usually raised in Python.
๐ŸŒ
Real Python
realpython.com โ€บ python-built-in-exceptions
Python's Built-in Exceptions: A Walkthrough With Examples โ€“ Real Python
March 18, 2026 - To better understand exceptions, say that you have a Python expression like a + b. This expression will work if a and b are both strings or numbers: ... In this example, the code works correctly because a and b are both numbers.
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ exception-handling
Python Exception Handling: Syntax, Usage, and Examples
You can use the try-except block to catch exceptions and execute alternative code. The basic syntax looks like this: ... Become a Python developer. Master Python from basics to advanced topics, including data structures, functions, classes, and error handling
๐ŸŒ
Python Land
python.land โ€บ home โ€บ language deep dives โ€บ python try except: examples and best practices
Python Try Except: Examples And Best Practices โ€ข Python Land Tutorial
January 29, 2026 - Exceptions have their own descriptive names. For example, if you try to divide a number by zero, you will get a ZeroDivisionError exception, which is also a subclass of the Exception class.
๐ŸŒ
Tutorials
zframez.com โ€บ tutorials โ€บ chapter 12: exception handling in python -techniques and examples
Chapter 12: Exception Handling in Python -Techniques and Examples - Tutorials
October 16, 2024 - Learn how to handle exceptions in Python using try-except blocks, handle multiple exceptions, create user-defined exceptions, and understand key errors like ValueError, TypeError, and FileNotFoundError
๐ŸŒ
Rollbar
rollbar.com โ€บ home โ€บ throwing exceptions in python
How to Throw Exceptions in Python | Rollbar
You use the โ€œraiseโ€ keyword to throw a Python exception manually. You can also add a message to describe the exception ยท Here is a simple example: Say you want the user to enter a date. The date has to be either today or in the future.
Published: May 22, 2026