đŸ•·ïž Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 71 (from laksa116)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

â„č Skipped - page is already crawled

📄
INDEXABLE
✅
CRAWLED
23 hours ago
đŸ€–
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://realpython.com/python-exceptions/
Last Crawled2026-04-16 08:18:11 (23 hours ago)
First Indexed2018-04-30 15:15:56 (7 years ago)
HTTP Status Code200
Meta TitlePython Exceptions: An Introduction – Real Python
Meta DescriptionIn this beginner tutorial, you'll learn what exceptions are good for in Python. You'll see how to raise exceptions and how to handle them with try ... except blocks.
Meta Canonicalnull
Boilerpipe Text
by Said van de Klundert Reading time estimate 24m basics python Python exceptions provide a mechanism for handling errors that occur during the execution of a program. Unlike syntax errors, which are detected by the parser, Python raises exceptions when an error occurs in syntactically correct code. Knowing how to raise, catch, and handle exceptions effectively helps to ensure your program behaves as expected, even when encountering errors. By the end of this tutorial, you’ll understand that: 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. Combining try , except , and pass allows your program to continue silently without handling the exception. In this tutorial, you’ll get to know Python exceptions and all relevant keywords for exception handling by walking through a practical example of handling a platform-related exception. Finally, you’ll also learn how to create your own custom Python exceptions. Take the Quiz: Test your knowledge with our interactive “Python Exceptions: An Introduction” quiz. You’ll receive a score upon completion to help you track your learning progress: Interactive Quiz Python Exceptions: An Introduction In this quiz, you'll test your understanding of Python exceptions. You'll cover the difference between syntax errors and exceptions and learn how to raise exceptions, make assertions, and use the try and except block. Understanding Exceptions and Syntax Errors Syntax errors occur when the parser detects an incorrect statement. Observe the following example: The arrow indicates where the parser ran into the syntax error . Additionally, the error message gives you a hint about what went wrong. In this example, there was one bracket too many. Remove it and run your code again: This time, you ran into an exception error . This type of error occurs whenever syntactically correct Python code results in an error. The last line of the message indicates what type of exception error you ran into. Instead of just writing exception error , Python details what type of exception error it encountered. In this case, it was a ZeroDivisionError . Python comes with various built-in exceptions as well as the possibility to create user-defined exceptions. Raising an Exception in Python There are scenarios where you might want to stop your program by raising an exception if a condition occurs. You can do this with the raise keyword: You can even complement the statement with a custom message. Assume that you’re writing a tiny toy program that expects only numbers up to 5 . You can raise an error when an unwanted condition occurs: In this example, you raised an Exception object and passed it an informative custom message. You built the message using an f-string and a self-documenting expression . When you run low.py , you’ll get the following output: The program comes to a halt and displays the exception to your terminal or REPL , offering you helpful clues about what went wrong. Note that the final call to print() never executed, because Python raised the exception before it got to that line of code. With the raise keyword, you can raise any exception object in Python and stop your program when an unwanted condition occurs. Debugging During Development With assert Before moving on to the most common way of working with exceptions in Python using the try 
 except block , you’ll take a quick look at an exception that’s a bit different than the others. Python offers a specific exception type that you should only use when debugging your program during development. This exception is the AssertionError . The AssertionError is special because you shouldn’t ever raise it yourself using raise . Instead, you use the assert keyword to check whether a condition is met and let Python raise the AssertionError if the condition isn’t met. The idea of an assertion is that your program should only attempt to run if certain conditions are in place. If Python checks your assertion and finds that the condition is True , then that is excellent! The program can continue. If the condition turns out to be False , then your program raises an AssertionError exception and stops right away: Revisit your tiny script, low.py , from the previous section . Currently, you’re explicitly raising an exception when a certain condition isn’t met: Assuming that you’ll handle this constraint safely for your production system, you could replace this conditional statement with an assertion for a quick way to retain this sanity check during development: If the number in your program is below 5 , then the assertion passes and your script continues with the next line of code. However, if you set number to a value higher than 5 —for example, 10 —then the outcome of the assertion will be False : In that case, Python raises an AssertionError that includes the message you passed, and ends the program execution: In this example, raising an AssertionError exception is the last thing that the program will do. The program will then come to halt and won’t continue. The call to print() that follows the assertion won’t execute. Using assertions in this way can be helpful when you’re debugging your program during development because it can be quite a fast and straightforward to add assertions into your code. However, you shouldn’t rely on assertions for catching crucial run conditions of your program in production. That’s because Python globally disables assertions when you run it in optimized mode using the -O and -OO command line options : In this run of your program, you used the -O command line option, which removes all assert statements. Therefore, your script ran all the way to the end and displayed a number that is dreadfully high! In production, your Python code may run using this optimized mode, which means that assertions aren’t a reliable way to handle runtime errors in production code. They can be quick and useful helpers when your debugging your code, but you should never use assertions to set crucial constraints for your program. If low.py should reliably fail when number is above 5 , then it’s best to stick with raising an exception . However, sometimes you might not want your program to fail when it encounters an exception, so how should you handle those situations? Handling Exceptions With the try and except Block In Python, you use the try and except block to catch and handle exceptions. Python executes code following the try statement as a normal part of the program. The code that follows the except statement is the program’s response to any exceptions in the preceding try clause: As you saw earlier, when syntactically correct code runs into an error, Python will raise an exception error. This exception error will crash the program if you don’t handle it. In the except clause, you can determine how your program should respond to exceptions. The following function can help you understand the try and except block: The linux_interaction() can only run on a Linux system. Python will raise a RuntimeError exception if you call it on an operating system other then Linux. You can give the function a try by adding the following code: The way you handled the error here is by handing out a pass . If you run this code on a macOS or Windows machine, then you get the following output: You got nothing in response. The good thing here is that your program didn’t crash. But letting an exception that occurred pass silently is bad practice. You should always at least know about and log if some type of exception occurred when you ran your code. To this end, you can change pass into something that generates an informative message: When you now execute this code on a macOS or Windows machine, you’ll see the message from your except block printed to the console: When an exception occurs in a program that runs this function, then the program will continue as well as inform you about the fact that the function call wasn’t successful. What you didn’t get to see was the type of error that Python raised as a result of the function call. In order to see exactly what went wrong, you’d need to catch the error that the function raised. The following code is an example where you capture the RuntimeError and output that message to your screen: In the except clause, you assign the RuntimeError to the temporary variable error —often also called err —so that you can access the exception object in the indented block. In this case, you’re printing the object’s string representation, which corresponds to the error message attached to the object. Running this function on a macOS or Windows machine outputs the following: The first message is the RuntimeError , informing you that Python can only execute the function on a Linux machine. The second message tells you which function wasn’t executed. In the example above, you called a function that you wrote yourself. When you executed the function, you caught the RuntimeError exception and printed it to your screen. Here’s another example where you open a file and use a built-in exception: If file.log doesn’t exist, then this block of code will output the following: This is an informative message, and your program will still continue to run. However, your except block will currently catch any exception, whether that’s related to not being able to open the file or not. You could lead yourself onto a confusing path if you see this message even when Python raises a completely unrelated exception. Therefore, it’s always best to be specific when you’re handling an exception. In the Python docs , you can see that there are a couple of built-in exceptions that you could raise in such a situation, for example: exception FileNotFoundError Raised when a file or directory is requested but doesn’t exist. Corresponds to errno ENOENT. ( Source ) You want to handle the situation when Python can’t find the requested file. To catch this type of exception and print it to screen, you could use the following code: In this case, if file.log doesn’t exist, then the output will be the following: You can have more than one function call in your try clause and anticipate catching various exceptions. Something to note here is that the code in the try clause will stop as soon as it encounters any one exception. Look at the following code. Here, you first call linux_interaction() and then try to open a file: If you run this code on a macOS or Windows machine, then you’ll see the following: Inside the try clause, you ran into an exception immediately and didn’t get to the part where you attempt to open file.log . Now look at what happens when you run the code on a Linux machine if the file doesn’t exist: Note that if you’re handling specific exceptions as you did above, then the order of the except clauses doesn’t matter too much. It’s all about which of the exceptions Python raises first. As soon as Python raises an exception, it checks the except clauses from top to bottom and executes the first matching one that it finds. Here are the key takeaways about using Python’s try 
 except statements: Python executes a try clause up until the point where it encounters the first exception. Inside the except clause—the exception handler—you determine how the program responds to the exception. You can anticipate multiple exceptions and differentiate how the program should respond to them. Avoid using bare except clauses , because they can hide unexpected exceptions. While using try together with except is probably the most common error handling that you’ll encounter, there’s more that you can do to fine-tune your program’s response to exceptions. Proceeding After a Successful Try With else You can use Python’s else statement to instruct a program to execute a certain block of code only in the absence of exceptions: Look at the following example: If you were to run this code on a Linux system, then the output would be the following: Because the program didn’t run into any exceptions, Python executed the code in the else clause. However, if you run this code on a macOS or Windows system, then you get a different output: The linux_interaction() function raised a RuntimeError . You’ve handled the exception, so your program doesn’t crash, and instead prints the exception message to the console. The code nested under the else clause, however, doesn’t execute, because Python encountered an exception during execution. Note that structuring your code like this is different from just adding the call to print() outside of the context of the try 
 except block: If you don’t nest the print() call under the else clause, then it’ll execute even if Python encounters the RuntimeError that you handle in the except block above. On a Linux system, the output would be the same, but on macOS or Windows, you’d get the following output: Nesting code under the else clause assures that it’ll only run when Python doesn’t encounter any exception when executing the try 
 except block. You can also create a nested try 
 except block inside the else clause and catch possible exceptions there as well: If you were to execute this code on a Linux machine, then you’d get the following result: From the output, you can see that linux_interaction() ran. Because Python encountered no exceptions, it attempted to open file.log . That file didn’t exist, but instead of letting the program crash, you caught the FileNotFoundError exception and printed a message to the console. Cleaning Up After Execution With finally Imagine that you always had to implement some sort of action to clean up after executing your code. Python enables you to do so using the finally clause: Have a look at the following example: In this code, Python will execute everything in the finally clause. It doesn’t matter if you encounter an exception somewhere in any of the try 
 except blocks. Running the code on a macOS or Windows machine will output the following: Note that the code inside the finally block will execute regardless of whether or not you’re handling the exceptions: You simplified the example code from above, but linux_interaction() still raises an exception on a macOS or Windows system. If you now run this code on an operating system other than Linux, then you’ll get the following output: Despite the fact that Python raised the RuntimeError , the code in the finally clause still executed and printed the message to your console. This can be helpful because even code outside of a try 
 except block won’t necessarily execute if your script encounters an unhandled exception. In that case, your program will terminate and the code after the try 
 except block will never run. However, Python will still execute the code inside of the finally clause. This helps you make sure that resources like file handles and database connections are cleaned up properly. Creating Custom Exceptions in Python With the large number of built-in exceptions that Python offers, you’ll likely find a fitting type when deciding which exception to raise. However, sometimes your code won’t fit the mold. Python makes it straightforward to create custom exception types by inheriting from a built-in exception. Think back to your linux_interaction() function: Using a RuntimeError isn’t a bad choice in this situation, but it would be nice if your exception name was a bit more specific. For this, you can create a custom exception: You generally create a custom exception in Python by inheriting from Exception , which is the base class for most built-in Python exceptions as well. You could also inherit from a different exception, but choosing Exception is usually the best choice. That’s really all that you need to do. In the code snippet above, you also added a docstring that describes the exception type and serves as the class body. While you can customize your exception object, you don’t need to do that. It’s often enough to give your custom Python exceptions a descriptive name, so you’ll know what happened when Python raises this exception in your code. Now that you’ve defined the custom exception, you can raise it like any other Python exception: If you now call linux_interaction() on macOS or Windows, then you’ll see that Python raises your custom exception: You could even use your custom PlatformException as a parent class for other custom exceptions that you could descriptively name for each of the platforms that users may run your code on. Conclusion At this point, you’re familiar with the basics of using Python exceptions. After seeing the difference between syntax errors and exceptions, you learned about various ways to raise, catch, and handle exceptions in Python. You also learned how you can create your own custom exceptions. In this article, you gained experience working with the following exception-related keywords: raise allows you to raise an exception at any time. assert enables you to verify if a certain condition is met and raises an exception if it isn’t. In the try clause, all statements are executed until an exception is encountered. except allows you to catch and handle the exception or exceptions that Python encountered in the try clause. else lets you code sections that should run only when Python encounters no exceptions in the try clause. finally enables you to execute sections of code that should always run, whether or not Python encountered any exceptions. You now understand the basic tools that Python offers for dealing with exceptions. If you’re curious about the topic and want to dive deeper, then take a look at the following tutorials: Python’s Built-in Exceptions: A Walkthrough With Examples Exception Groups and except* Python raise : Effectively Raising Exceptions in Your Code How to Catch Multiple Exception in Python Understanding the Python Traceback LBYL vs EAFP: Preventing or Handling Errors in Python What’s your favorite aspect of exception handling in Python? Share your thoughts in the comments below. Frequently Asked Questions Now that you have some experience with Python exceptions, you can use the questions and answers below to check your understanding and recap what you’ve learned. These FAQs are related to the most important concepts you’ve covered in this tutorial. Click the Show/Hide toggle beside each question to reveal the answer. Exceptions in Python are errors that occur during the execution of a program, disrupting the normal flow of the program. You handle exceptions in Python using a try 
 except block. Python executes the code in the try block and if an exception occurs, it switches to executing the code in the except block to handle the exception. However, only the exceptions that are explicitly specified in the except block will be handled. If an exception is not caught, it’ll propagate up the call stack and may result in the termination of your program. To catch all exceptions in Python, you can use a bare except clause or write except Exception , but it’s recommended to catch specific exceptions to avoid masking unexpected errors. In a try 
 except block, Python executes the code under try and if an exception occurs, it immediately jumps to the except block to handle it, allowing the program to continue running. Using try 
 except with pass allows the program to ignore the exception and continue execution without taking any specific action in response to the error. However, this practice can hide potential issues, making it harder to debug and maintain the code, so use it with caution. It’s generally better to either handle the exception explicitly or log it for debugging purposes. You raise an exception in Python using the raise keyword followed by an exception object, which can include a custom message. You can use the assert keyword to check if a condition is true during development. If the condition is false, it raises an AssertionError , which can help with debugging. Note that assertions can be disabled by running Python with the -O (optimize) flag. Therefore, you shouldn’t rely on assertions for critical checks in production code, as they may be ignored. The finally clause contains code that will always execute after a try block, regardless of whether an exception was raised or not, ensuring necessary cleanup actions occur. Take the Quiz: Test your knowledge with our interactive “Python Exceptions: An Introduction” quiz. You’ll receive a score upon completion to help you track your learning progress: Interactive Quiz Python Exceptions: An Introduction In this quiz, you'll test your understanding of Python exceptions. You'll cover the difference between syntax errors and exceptions and learn how to raise exceptions, make assertions, and use the try and except block.
Markdown
[![Real Python](https://realpython.com/static/real-python-logo.893c30edea53.svg)](https://realpython.com/) - [Start Here](https://realpython.com/start-here/) - [Learn Python](https://realpython.com/python-exceptions/) [Python Tutorials → In-depth articles and video courses](https://realpython.com/search?kind=article&kind=course&order=newest) [Learning Paths → Guided study plans for accelerated learning](https://realpython.com/learning-paths/) [Quizzes & Exercises → Check your learning progress](https://realpython.com/quizzes/) [Browse Topics → Focus on a specific area or skill level](https://realpython.com/tutorials/all/) [Community Chat → Learn with other Pythonistas](https://realpython.com/community/) [Office Hours → Live Q\&A calls with Python experts](https://realpython.com/office-hours/) [Live Courses → Live, instructor-led Python courses](https://realpython.com/live/) [Podcast → Hear what’s new in the world of Python](https://realpython.com/podcasts/rpp/) [Books → Round out your knowledge and learn offline](https://realpython.com/products/books/) [Reference → Concise definitions for common Python terms](https://realpython.com/ref/) [Code Mentor →Beta Personalized code assistance & learning tools](https://realpython.com/mentor/) [Unlock All Content →](https://realpython.com/account/join/) - [More](https://realpython.com/python-exceptions/) [Learner Stories](https://realpython.com/learner-stories/) [Python Newsletter](https://realpython.com/newsletter/) [Python Job Board](https://www.pythonjobshq.com/) [Meet the Team](https://realpython.com/team/) [Become a Contributor](https://realpython.com/jobs/) - [Search](https://realpython.com/search "Search") - [Join](https://realpython.com/account/join/) - [Sign‑In](https://realpython.com/account/login/?next=%2Fpython-exceptions%2F) [Browse Topics](https://realpython.com/tutorials/all/) [Guided Learning Paths](https://realpython.com/learning-paths/) [Basics](https://realpython.com/search?level=basics) [Intermediate](https://realpython.com/search?level=intermediate) [Advanced](https://realpython.com/search?level=advanced) *** [ai](https://realpython.com/tutorials/ai/) [algorithms](https://realpython.com/tutorials/algorithms/) [api](https://realpython.com/tutorials/api/) [best-practices](https://realpython.com/tutorials/best-practices/) [career](https://realpython.com/tutorials/career/) [community](https://realpython.com/tutorials/community/) [databases](https://realpython.com/tutorials/databases/) [data-science](https://realpython.com/tutorials/data-science/) [data-structures](https://realpython.com/tutorials/data-structures/) [data-viz](https://realpython.com/tutorials/data-viz/) [devops](https://realpython.com/tutorials/devops/) [django](https://realpython.com/tutorials/django/) [docker](https://realpython.com/tutorials/docker/) [editors](https://realpython.com/tutorials/editors/) [flask](https://realpython.com/tutorials/flask/) [front-end](https://realpython.com/tutorials/front-end/) [gamedev](https://realpython.com/tutorials/gamedev/) [gui](https://realpython.com/tutorials/gui/) [machine-learning](https://realpython.com/tutorials/machine-learning/) [news](https://realpython.com/tutorials/news/) [numpy](https://realpython.com/tutorials/numpy/) [projects](https://realpython.com/tutorials/projects/) [python](https://realpython.com/tutorials/python/) [stdlib](https://realpython.com/tutorials/stdlib/) [testing](https://realpython.com/tutorials/testing/) [tools](https://realpython.com/tutorials/tools/) [web-dev](https://realpython.com/tutorials/web-dev/) [web-scraping](https://realpython.com/tutorials/web-scraping/) [Table of Contents](https://realpython.com/python-exceptions/#toc) - [Understanding Exceptions and Syntax Errors](https://realpython.com/python-exceptions/#understanding-exceptions-and-syntax-errors) - [Raising an Exception in Python](https://realpython.com/python-exceptions/#raising-an-exception-in-python) - [Debugging During Development With assert](https://realpython.com/python-exceptions/#debugging-during-development-with-assert) - [Handling Exceptions With the try and except Block](https://realpython.com/python-exceptions/#handling-exceptions-with-the-try-and-except-block) - [Proceeding After a Successful Try With else](https://realpython.com/python-exceptions/#proceeding-after-a-successful-try-with-else) - [Cleaning Up After Execution With finally](https://realpython.com/python-exceptions/#cleaning-up-after-execution-with-finally) - [Creating Custom Exceptions in Python](https://realpython.com/python-exceptions/#creating-custom-exceptions-in-python) - [Conclusion](https://realpython.com/python-exceptions/#conclusion) - [Frequently Asked Questions](https://realpython.com/python-exceptions/#frequently-asked-questions) Mark as Completed Share Recommended Courses [![An Introduction to Python Exceptions](https://files.realpython.com/media/Python_Exceptions_Watermark.47f814fbeced.jpg) Raising and Handling Python Exceptions 1h 2m · 13 lessons](https://realpython.com/courses/raising-handling-exceptions/) View 1 more course - [Introduction to Python Exceptions](https://realpython.com/courses/introduction-python-exceptions/) 13m · 3 lessons ![An Introduction to Python Exceptions](https://files.realpython.com/media/Python_Exceptions_Watermark.47f814fbeced.jpg) # Python Exceptions: An Introduction by [Said van de Klundert](https://realpython.com/python-exceptions/#author) Reading time estimate 24m [35 Comments](https://realpython.com/python-exceptions/#reader-comments) [basics](https://realpython.com/tutorials/basics/) [python](https://realpython.com/tutorials/python/) Mark as Completed Share Table of Contents - [Understanding Exceptions and Syntax Errors](https://realpython.com/python-exceptions/#understanding-exceptions-and-syntax-errors) - [Raising an Exception in Python](https://realpython.com/python-exceptions/#raising-an-exception-in-python) - [Debugging During Development With assert](https://realpython.com/python-exceptions/#debugging-during-development-with-assert) - [Handling Exceptions With the try and except Block](https://realpython.com/python-exceptions/#handling-exceptions-with-the-try-and-except-block) - [Proceeding After a Successful Try With else](https://realpython.com/python-exceptions/#proceeding-after-a-successful-try-with-else) - [Cleaning Up After Execution With finally](https://realpython.com/python-exceptions/#cleaning-up-after-execution-with-finally) - [Creating Custom Exceptions in Python](https://realpython.com/python-exceptions/#creating-custom-exceptions-in-python) - [Conclusion](https://realpython.com/python-exceptions/#conclusion) - [Frequently Asked Questions](https://realpython.com/python-exceptions/#frequently-asked-questions) [Remove ads](https://realpython.com/account/join/) Recommended Courses [Raising and Handling Python Exceptions](https://realpython.com/courses/raising-handling-exceptions/) (1h 2m) 1 more course - [Introduction to Python Exceptions](https://realpython.com/courses/introduction-python-exceptions/) (13m) Python exceptions provide a mechanism for handling errors that occur during the execution of a program. Unlike syntax errors, which are detected by the parser, Python raises exceptions when an error occurs in syntactically correct code. Knowing how to raise, catch, and handle exceptions effectively helps to ensure your program behaves as expected, even when encountering errors. **By the end of this tutorial, you’ll understand that:** - 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. - Combining `try`, `except`, and `pass` allows your program to **continue silently** without handling the exception. In this tutorial, you’ll get to know Python exceptions and all relevant keywords for exception handling by walking through a practical example of handling a platform-related exception. Finally, you’ll also learn how to create your own custom Python exceptions. **Get Your Code:** [Click here to download the free sample code](https://realpython.com/bonus/python-exceptions-code/) that shows you how exceptions work in Python. ***Take the Quiz:*** Test your knowledge with our interactive “Python Exceptions: An Introduction” quiz. You’ll receive a score upon completion to help you track your learning progress: *** [![An Introduction to Python Exceptions](https://files.realpython.com/media/Python_Exceptions_Watermark.47f814fbeced.jpg)](https://realpython.com/quizzes/python-exceptions/) **Interactive Quiz** [Python Exceptions: An Introduction](https://realpython.com/quizzes/python-exceptions/) In this quiz, you'll test your understanding of Python exceptions. You'll cover the difference between syntax errors and exceptions and learn how to raise exceptions, make assertions, and use the try and except block. ## Understanding Exceptions and Syntax Errors [Syntax errors](https://realpython.com/invalid-syntax-python/) occur when the parser detects an incorrect statement. Observe the following example: Python Traceback ``` ``` The arrow indicates where the parser ran into the **syntax error**. Additionally, the error message gives you a hint about what went wrong. In this example, there was one bracket too many. Remove it and run your code again: Python ``` ``` This time, you ran into an **exception error**. This type of error occurs whenever syntactically correct Python code results in an error. The last line of the message indicates what type of exception error you ran into. Instead of just writing *exception error*, Python details what *type* of exception error it encountered. In this case, it was a `ZeroDivisionError`. Python comes with [various built-in exceptions](https://docs.python.org/3/library/exceptions.html) as well as the possibility to create user-defined exceptions. [Remove ads](https://realpython.com/account/join/) ## Raising an Exception in Python There are scenarios where you might want to stop your program by raising an exception if a condition occurs. You can do this with the [`raise`](https://realpython.com/python-raise-exception/) keyword: [![Illustration of raise statement usage](https://files.realpython.com/media/raise.3931e8819e08.png)](https://files.realpython.com/media/raise.3931e8819e08.png) You can even complement the statement with a custom message. Assume that you’re writing a tiny toy program that expects only numbers up to `5`. You can raise an error when an unwanted condition occurs: Python `low.py` ``` ``` In this example, you raised an `Exception` object and passed it an informative custom message. You built the message using an [f-string](https://realpython.com/python-f-strings/) and a [self-documenting expression](https://realpython.com/python-f-strings/#self-documenting-expressions-for-debugging). When you run `low.py`, you’ll get the following output: Python Traceback ``` ``` The program comes to a halt and displays the exception to your [terminal](https://realpython.com/terminal-commands/) or [REPL](https://realpython.com/python-repl/), offering you helpful clues about what went wrong. Note that the final call to [`print()`](https://realpython.com/python-print/) never executed, because Python raised the exception before it got to that line of code. With the `raise` keyword, you can raise any exception object in Python and stop your program when an unwanted condition occurs. ## Debugging During Development With `assert` Before moving on to the most common way of working with exceptions in Python using [the `try` 
 `except` block](https://realpython.com/python-exceptions/#handling-exceptions-with-the-try-and-except-block), you’ll take a quick look at an exception that’s a bit different than the others. Python offers a specific exception type that you should only use when debugging your program during development. This exception is the `AssertionError`. The `AssertionError` is special because you shouldn’t ever raise it yourself using `raise`. Instead, you use [the `assert` keyword](https://dbader.org/blog/python-assert-tutorial) to check whether a condition is met and let Python raise the `AssertionError` if the condition isn’t met. The idea of an assertion is that your program should only attempt to run if certain conditions are in place. If Python checks your assertion and finds that the condition is `True`, then that is excellent! The program can continue. If the condition turns out to be `False`, then your program raises an `AssertionError` exception and stops right away: [![Python assert statement](https://files.realpython.com/media/assert.f6d344f0c0b4.png)](https://files.realpython.com/media/assert.f6d344f0c0b4.png) Revisit your tiny script, `low.py`, from [the previous section](https://realpython.com/python-exceptions/#raising-an-exception-in-python). Currently, you’re explicitly raising an exception when a certain condition isn’t met: Python `low.py` ``` ``` Assuming that you’ll handle this constraint safely for your production system, you could replace this [conditional statement](https://realpython.com/python-conditional-statements/) with an assertion for a quick way to retain this sanity check during development: Python `low.py` ``` ``` If the `number` in your program is below `5`, then the assertion passes and your script continues with the next line of code. However, if you set `number` to a value higher than `5`—for example, `10`—then the outcome of the assertion will be `False`: Python `low.py` ``` ``` In that case, Python raises an `AssertionError` that includes the message you passed, and ends the program execution: Shell ``` ``` In this example, raising an `AssertionError` exception is the last thing that the program will do. The program will then come to halt and won’t continue. The call to `print()` that follows the assertion won’t execute. Using assertions in this way can be helpful when you’re debugging your program during development because it can be quite a fast and straightforward to add assertions into your code. However, you shouldn’t rely on assertions for catching crucial run conditions of your program in production. That’s because Python globally disables assertions when you run it in optimized mode using the [`-O` and `-OO` command line options](https://docs.python.org/3/using/cmdline.html#cmdoption-O): Shell ``` ``` In this run of your program, you used the `-O` command line option, which removes all `assert` statements. Therefore, your script ran all the way to the end and displayed a number that is *dreadfully* high\! **Note:** Alternatively, you can also disable assertions through the [`PYTHONOPTIMIZE` environment variable](https://docs.python.org/3/using/cmdline.html#envvar-PYTHONOPTIMIZE). In production, your Python code may run using this optimized mode, which means that assertions aren’t a reliable way to handle runtime errors in production code. They can be quick and useful helpers when your debugging your code, but you should never use assertions to set crucial constraints for your program. If `low.py` should reliably fail when `number` is above `5`, then it’s best to stick with [raising an exception](https://realpython.com/python-exceptions/#raising-an-exception-in-python). However, sometimes you might not want your program to fail when it encounters an exception, so how should you handle those situations? [Remove ads](https://realpython.com/account/join/) ## Handling Exceptions With the `try` and `except` Block In Python, you use the `try` and `except` block to catch and handle exceptions. Python executes code following the `try` statement as a normal part of the program. The code that follows the `except` statement is the program’s response to any exceptions in the preceding `try` clause: [![Diagram showing try and except statements](https://files.realpython.com/media/try_except.c94eabed2c59.png)](https://files.realpython.com/media/try_except.c94eabed2c59.png) As you saw earlier, when syntactically correct code runs into an error, Python will raise an exception error. This exception error will crash the program if you don’t handle it. In the `except` clause, you can determine how your program should respond to exceptions. The following function can help you understand the `try` and `except` block: Python `linux_interaction.py` ``` ``` The `linux_interaction()` can only run on a Linux system. Python will raise a `RuntimeError` exception if you call it on an operating system other then Linux. **Note:** Picking the right exception type can sometimes be tricky. Python comes with [many built-in exceptions](https://docs.python.org/3/library/exceptions.html#concrete-exceptions) that are [hierarchically related](https://docs.python.org/3/library/exceptions.html#exception-hierarchy), so if you browse the documentation, you’re likely to find a fitting one. Python even groups some of the exceptions into categories, such as [warnings](https://docs.python.org/3/library/exceptions.html#warnings) that you should use to indicate warning conditions, and [OS exceptions](https://docs.python.org/3/library/exceptions.html#os-exceptions) that Python raises depending on system error codes. If you still didn’t find a fitting exception, then you can [create a custom exception](https://realpython.com/python-exceptions/#creating-custom-exceptions-in-python). You can give the function a `try` by adding the following code: Python `linux_interaction.py` ``` ``` The way you handled the error here is by handing out a `pass`. If you run this code on a macOS or Windows machine, then you get the following output: Shell ``` $ python linux_interaction.py ``` You got nothing in response. The good thing here is that your program didn’t crash. But letting an exception that occurred pass silently is bad practice. You should always at least know about and [log](https://realpython.com/python-logging/) if some type of exception occurred when you ran your code. To this end, you can change `pass` into something that generates an informative message: Python `linux_interaction.py` ``` ``` When you now execute this code on a macOS or Windows machine, you’ll see the message from your `except` block printed to the console: Shell ``` ``` When an exception occurs in a program that runs this function, then the program will continue as well as inform you about the fact that the function call wasn’t successful. What you didn’t get to see was the type of error that Python raised as a result of the function call. In order to see exactly what went wrong, you’d need to catch the error that the function raised. The following code is an example where you capture the `RuntimeError` and output that message to your screen: Python `linux_interaction.py` ``` ``` In the `except` clause, you assign the `RuntimeError` to the temporary variable `error`—often also called `err`—so that you can access the exception object in the indented block. In this case, you’re printing the object’s string representation, which corresponds to the error message attached to the object. Running this function on a macOS or Windows machine outputs the following: Shell ``` ``` The first message is the `RuntimeError`, informing you that Python can only execute the function on a Linux machine. The second message tells you which function wasn’t executed. In the example above, you called a function that you wrote yourself. When you executed the function, you caught the `RuntimeError` exception and printed it to your screen. Here’s another example where you open a file and use a built-in exception: Python `open_file.py` ``` ``` If `file.log` doesn’t exist, then this block of code will output the following: Shell ``` ``` This is an informative message, and your program will still continue to run. However, your `except` block will currently catch *any* exception, whether that’s related to not being able to open the file or not. You could lead yourself onto a confusing path if you see this message even when Python raises a completely unrelated exception. Therefore, it’s always best to be *specific* when you’re handling an exception. In the [Python docs](https://docs.python.org/3/library/exceptions.html), you can see that there are a couple of built-in exceptions that you could raise in such a situation, for example: > *exception* `FileNotFoundError` > > Raised when a file or directory is requested but doesn’t exist. Corresponds to errno ENOENT. ([Source](https://docs.python.org/3/library/exceptions.html#FileNotFoundError)) You want to handle the situation when Python can’t find the requested file. To catch this type of exception and print it to screen, you could use the following code: Python `open_file.py` ``` ``` In this case, if `file.log` doesn’t exist, then the output will be the following: Shell ``` ``` You can have more than one function call in your `try` clause and anticipate catching various exceptions. Something to note here is that the code in the `try` clause will stop as soon as it encounters any one exception. **Warning:** When you use a bare `except` clause, then Python catches any exception that inherits from `Exception`—which are most built-in exceptions! Catching the parent class, `Exception`, hides all errors—even those which you didn’t expect at all. This is why you should avoid bare `except` clauses in your Python programs. Instead, you’ll want to refer to *specific exception classes* that you want to catch and handle. You can learn more about why this is a good idea [in this tutorial](https://realpython.com/the-most-diabolical-python-antipattern/). Look at the following code. Here, you first call `linux_interaction()` and then try to open a file: Python `linux_interaction.py` ``` ``` If you run this code on a macOS or Windows machine, then you’ll see the following: Shell ``` ``` Inside the `try` clause, you ran into an exception immediately and didn’t get to the part where you attempt to open `file.log`. Now look at what happens when you run the code on a Linux machine if the file doesn’t exist: Shell ``` ``` Note that if you’re handling specific exceptions as you did above, then the order of the `except` clauses doesn’t matter too much. It’s all about which of the exceptions Python raises first. As soon as Python raises an exception, it checks the except clauses from top to bottom and executes the first matching one that it finds. Here are the key takeaways about using Python’s `try` 
 `except` statements: - Python executes a `try` clause up until the point where it encounters the first exception. - Inside the `except` clause—the exception handler—you determine how the program responds to the exception. - You can anticipate [multiple exceptions](https://realpython.com/python-catch-multiple-exceptions/) and differentiate how the program should respond to them. - [Avoid using bare `except` clauses](https://realpython.com/the-most-diabolical-python-antipattern/), because they can hide unexpected exceptions. While using `try` together with `except` is probably the most common error handling that you’ll encounter, there’s more that you can do to fine-tune your program’s response to exceptions. [Remove ads](https://realpython.com/account/join/) ## Proceeding After a Successful Try With `else` You can use Python’s `else` statement to instruct a program to execute a certain block of code only in the absence of exceptions: [![Diagram of try, except, and else statements in Python](https://files.realpython.com/media/try_except_else.703aaeeb63d3.png)](https://files.realpython.com/media/try_except_else.703aaeeb63d3.png) Look at the following example: Python `linux_interaction.py` ``` ``` If you were to run this code on a Linux system, then the output would be the following: Shell ``` ``` Because the program didn’t run into *any* exceptions, Python executed the code in the `else` clause. However, if you run this code on a macOS or Windows system, then you get a different output: Shell ``` ``` The `linux_interaction()` function raised a `RuntimeError`. You’ve handled the exception, so your program doesn’t crash, and instead prints the exception message to the console. The code nested under the `else` clause, however, doesn’t execute, because Python encountered an exception during execution. Note that structuring your code like this is different from just adding the call to `print()` outside of the context of the `try` 
 `except` block: Python `linux_interaction.py` ``` ``` If you don’t nest the `print()` call under the `else` clause, then it’ll execute even if Python encounters the `RuntimeError` that you handle in the `except` block above. On a Linux system, the output would be the same, but on macOS or Windows, you’d get the following output: Shell ``` ``` Nesting code under the `else` clause assures that it’ll only run when Python doesn’t encounter any exception when executing the `try` 
 `except` block. You can also create a nested `try` 
 `except` block inside the `else` clause and catch possible exceptions there as well: Python `linux_interaction.py` ``` ``` If you were to execute this code on a Linux machine, then you’d get the following result: Shell ``` ``` From the output, you can see that `linux_interaction()` ran. Because Python encountered no exceptions, it attempted to open `file.log`. That file didn’t exist, but instead of letting the program crash, you caught the `FileNotFoundError` exception and printed a message to the console. [Remove ads](https://realpython.com/account/join/) ## Cleaning Up After Execution With `finally` Imagine that you always had to implement some sort of action to clean up after executing your code. Python enables you to do so using the `finally` clause: [![Diagram explaining try except else finally statements](https://files.realpython.com/media/try_except_else_finally.a7fac6c36c55.png)](https://files.realpython.com/media/try_except_else_finally.a7fac6c36c55.png) Have a look at the following example: Python `linux_interaction.py` ``` ``` In this code, Python will execute everything in the `finally` clause. It doesn’t matter if you encounter an exception somewhere in any of the `try` 
 `except` blocks. Running the code on a macOS or Windows machine will output the following: Shell ``` ``` Note that the code inside the `finally` block will execute regardless of whether or not you’re handling the exceptions: Python `linux_interaction.py` ``` ``` You simplified the example code from above, but `linux_interaction()` still raises an exception on a macOS or Windows system. If you now run this code on an operating system other than Linux, then you’ll get the following output: Shell ``` ``` Despite the fact that Python raised the `RuntimeError`, the code in the `finally` clause still executed and printed the message to your console. This can be helpful because even code outside of a `try`
 `except` block won’t necessarily execute if your script encounters an unhandled exception. In that case, your program will terminate and the code *after* the `try` 
 `except` block will never run. However, Python will still execute the code inside of the `finally` clause. This helps you make sure that resources like [file handles](https://realpython.com/why-close-file-python/) and [database connections](https://realpython.com/python-sql-libraries/) are cleaned up properly. ## Creating Custom Exceptions in Python With the large number of built-in exceptions that Python offers, you’ll likely find a fitting type when deciding which exception to raise. However, sometimes your code won’t fit the mold. Python makes it straightforward to create custom exception types by inheriting from a built-in exception. Think back to your `linux_interaction()` function: Python `linux_interaction.py` ``` ``` Using a [`RuntimeError`](https://docs.python.org/3/library/exceptions.html#RuntimeError) isn’t a bad choice in this situation, but it would be nice if your exception name was a bit more specific. For this, you can create a custom exception: Python `linux_interaction.py` ``` ``` You generally create a custom exception in Python by inheriting from `Exception`, which is the base class for most built-in Python exceptions as well. You could also inherit from a different exception, but choosing `Exception` is usually the best choice. That’s really all that you need to do. In the code snippet above, you also added a [docstring](https://realpython.com/documenting-python-code/) that describes the exception type and serves as the class body. **Note:** Python requires some indented code in the body of your class. Alternatively to using the docstring, you could’ve also used [`pass`](https://realpython.com/python-pass/) or [the ellipsis (`...`)](https://realpython.com/python-ellipsis/). However, adding a descriptive docstring adds the most value to your custom exception. To learn how to write effective docstrings, check out [How to Write Docstrings in Python](https://realpython.com/how-to-write-docstrings-in-python/). While you can customize your exception object, you don’t need to do that. It’s often enough to give your custom Python exceptions a descriptive name, so you’ll know what happened when Python raises this exception in your code. Now that you’ve defined the custom exception, you can raise it like any other Python exception: Python `linux_interaction.py` ``` ``` If you now call `linux_interaction()` on macOS or Windows, then you’ll see that Python raises your custom exception: Shell ``` ``` You could even use your custom `PlatformException` as a parent class for other custom exceptions that you could descriptively name for each of the platforms that users may run your code on. [Remove ads](https://realpython.com/account/join/) ## Conclusion At this point, you’re familiar with the basics of using Python exceptions. After seeing the difference between syntax errors and exceptions, you learned about various ways to raise, catch, and handle exceptions in Python. You also learned how you can create your own custom exceptions. In this article, you gained experience working with the following exception-related keywords: - `raise` allows you to raise an exception at any time. - `assert` enables you to verify if a certain condition is met and raises an exception if it isn’t. - In the `try` clause, all statements are executed until an exception is encountered. - `except` allows you to catch and handle the exception or exceptions that Python encountered in the `try` clause. - `else` lets you code sections that should run only when Python encounters no exceptions in the `try` clause. - `finally` enables you to execute sections of code that should always run, whether or not Python encountered any exceptions. **Get Your Code:** [Click here to download the free sample code](https://realpython.com/bonus/python-exceptions-code/) that shows you how exceptions work in Python. You now understand the basic tools that Python offers for dealing with exceptions. If you’re curious about the topic and want to dive deeper, then take a look at the following tutorials: - [Python’s Built-in Exceptions: A Walkthrough With Examples](https://realpython.com/python-built-in-exceptions/) - [Exception Groups and `except*`](https://realpython.com/python311-exception-groups/#exception-groups-and-except-in-python-311) - [Python `raise`: Effectively Raising Exceptions in Your Code](https://realpython.com/python-raise-exception/) - [How to Catch Multiple Exception in Python](https://realpython.com/python-catch-multiple-exceptions/) - [Understanding the Python Traceback](https://realpython.com/python-traceback/) - [LBYL vs EAFP: Preventing or Handling Errors in Python](https://realpython.com/python-lbyl-vs-eafp/) What’s your favorite aspect of exception handling in Python? Share your thoughts in the comments below. ## Frequently Asked Questions Now that you have some experience with Python exceptions, you can use the questions and answers below to check your understanding and recap what you’ve learned. These FAQs are related to the most important concepts you’ve covered in this tutorial. Click the *Show/Hide* toggle beside each question to reveal the answer. ****What are exceptions in Python?****Show/Hide Exceptions in Python are errors that occur during the execution of a program, disrupting the normal flow of the program. ****How are exceptions handled in Python?****Show/Hide You handle exceptions in Python using a `try` 
 `except` block. Python executes the code in the `try` block and if an exception occurs, it switches to executing the code in the `except` block to handle the exception. However, only the exceptions that are explicitly specified in the `except` block will be handled. If an exception is not caught, it’ll propagate up the call stack and may result in the termination of your program. ****How do you catch all exceptions in Python?****Show/Hide To catch all exceptions in Python, you can use a bare `except` clause or write `except Exception`, but it’s recommended to catch specific exceptions to avoid masking unexpected errors. ****How does `try` 
 `except` in Python work?****Show/Hide In a `try` 
 `except` block, Python executes the code under `try` and if an exception occurs, it immediately jumps to the `except` block to handle it, allowing the program to continue running. ****What does `try` 
 `except` `pass` do in Python?****Show/Hide Using `try` 
 `except` with `pass` allows the program to ignore the exception and continue execution without taking any specific action in response to the error. However, this practice can hide potential issues, making it harder to debug and maintain the code, so use it with caution. It’s generally better to either handle the exception explicitly or log it for debugging purposes. ****How do you raise an exception in Python?****Show/Hide You raise an exception in Python using the `raise` keyword followed by an exception object, which can include a custom message. ****What is the purpose of using `assert` in Python?****Show/Hide You can use the `assert` keyword to check if a condition is true during development. If the condition is false, it raises an `AssertionError`, which can help with debugging. Note that assertions can be disabled by running Python with the `-O` (optimize) flag. Therefore, you shouldn’t rely on assertions for critical checks in production code, as they may be ignored. ****What is the role of the `finally` clause in exception handling?****Show/Hide The `finally` clause contains code that will always execute after a `try` block, regardless of whether an exception was raised or not, ensuring necessary cleanup actions occur. ***Take the Quiz:*** Test your knowledge with our interactive “Python Exceptions: An Introduction” quiz. You’ll receive a score upon completion to help you track your learning progress: *** [![An Introduction to Python Exceptions](https://files.realpython.com/media/Python_Exceptions_Watermark.47f814fbeced.jpg)](https://realpython.com/quizzes/python-exceptions/) **Interactive Quiz** [Python Exceptions: An Introduction](https://realpython.com/quizzes/python-exceptions/) In this quiz, you'll test your understanding of Python exceptions. You'll cover the difference between syntax errors and exceptions and learn how to raise exceptions, make assertions, and use the try and except block. Mark as Completed Share Recommended Courses [Raising and Handling Python Exceptions](https://realpython.com/courses/raising-handling-exceptions/) (1h 2m) 1 more course - [Introduction to Python Exceptions](https://realpython.com/courses/introduction-python-exceptions/) (13m) 🐍 Python Tricks 💌 Get a short & sweet **Python Trick** delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team. ![Python Tricks Dictionary Merge](https://realpython.com/static/pytrick-dict-merge.4201a0125a5e.png) About **Said van de Klundert** [![Said van de Klundert](https://realpython.com/cdn-cgi/image/width=335,height=335,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/AAEAAQAAAAAAAAQiAAAAJGJmOGNlMzMzLTg1MmEtNGQwYy1hZDkyLTEwYzI0MjRjNTZkOA_2.8aa7cc5bf1f9.jpg) ![Said van de Klundert](https://realpython.com/cdn-cgi/image/width=335,height=335,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/AAEAAQAAAAAAAAQiAAAAJGJmOGNlMzMzLTg1MmEtNGQwYy1hZDkyLTEwYzI0MjRjNTZkOA_2.8aa7cc5bf1f9.jpg)](https://realpython.com/team/svdklundert/) Said is a network engineer, Python enthusiast, and a guest author at Real Python. [» More about Said](https://realpython.com/team/svdklundert/) *** *Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:* [![Adriana Cutenco](https://realpython.com/cdn-cgi/image/width=900,height=900,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/acutenco.676e4197c133.jpg)](https://realpython.com/team/acutenco/) [Adriana](https://realpython.com/team/acutenco/) [![Brenda Weleschuk](https://realpython.com/cdn-cgi/image/width=320,height=320,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/IMG_3324_1.50b309355fc1.jpg)](https://realpython.com/team/bweleschuk/) [Brenda](https://realpython.com/team/bweleschuk/) [![Bartosz ZaczyƄski](https://realpython.com/cdn-cgi/image/width=1694,height=1694,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/coders_lab_2109368.259b1599fbee.jpg)](https://realpython.com/team/bzaczynski/) [Bartosz](https://realpython.com/team/bzaczynski/) [![Geir Arne Hjelle](https://realpython.com/cdn-cgi/image/width=800,height=800,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/gahjelle.470149ee709e.jpg)](https://realpython.com/team/gahjelle/) [Geir Arne](https://realpython.com/team/gahjelle/) [![Joanna Jablonski](https://realpython.com/cdn-cgi/image/width=800,height=800,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/jjablonksi-avatar.e37c4f83308e.jpg)](https://realpython.com/team/jjablonski/) [Joanna](https://realpython.com/team/jjablonski/) [![Kate Finegan](https://realpython.com/cdn-cgi/image/width=400,height=400,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/VZxEtUor_400x400.7169c68e3950.jpg)](https://realpython.com/team/kfinegan/) [Kate](https://realpython.com/team/kfinegan/) [![Martin Breuss](https://realpython.com/cdn-cgi/image/width=456,height=456,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/martin_breuss_python_square.efb2b07faf9f.jpg)](https://realpython.com/team/mbreuss/) [Martin](https://realpython.com/team/mbreuss/) Master Real-World Python Skills With Unlimited Access to Real Python ![Locked learning resources](https://realpython.com/static/videos/lesson-locked.f5105cfd26db.svg) **Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:** [Level Up Your Python Skills »](https://realpython.com/account/join/?utm_source=rp_article_footer&utm_content=python-exceptions) Master Real-World Python Skills With Unlimited Access to Real Python ![Locked learning resources](https://realpython.com/static/videos/lesson-locked.f5105cfd26db.svg) **Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:** [Level Up Your Python Skills »](https://realpython.com/account/join/?utm_source=rp_article_footer&utm_content=python-exceptions) What Do You Think? **Rate this article:** [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Frealpython.com%2Fpython-exceptions%2F) [Twitter](https://twitter.com/intent/tweet/?text=Interesting%20Python%20article%20on%20%40realpython%3A%20Python%20Exceptions%3A%20An%20Introduction&url=https%3A%2F%2Frealpython.com%2Fpython-exceptions%2F) [Bluesky](https://bsky.app/intent/compose?text=Interesting%20Python%20article%20on%20%40realpython.com%3A%20Python%20Exceptions%3A%20An%20Introduction%20https%3A%2F%2Frealpython.com%2Fpython-exceptions%2F) [Facebook](https://facebook.com/sharer/sharer.php?u=https%3A%2F%2Frealpython.com%2Fpython-exceptions%2F) [Email](mailto:?subject=Python%20article%20for%20you&body=Python%20Exceptions%3A%20An%20Introduction%20on%20Real%20Python%0A%0Ahttps%3A%2F%2Frealpython.com%2Fpython-exceptions%2F%0A) What’s your \#1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know. **Commenting Tips:** The most useful comments are those written with the goal of learning from or helping out other students. [Get tips for asking good questions](https://realpython.com/python-beginner-tips/#tip-9-ask-good-questions) and [get answers to common questions in our support portal](https://support.realpython.com/). *** Looking for a real-time conversation? Visit the [Real Python Community Chat](https://realpython.com/community/) or join the next [“Office Hours” Live Q\&A Session](https://realpython.com/office-hours/). Happy Pythoning\! Keep Learning Related Topics: [basics](https://realpython.com/tutorials/basics/) [python](https://realpython.com/tutorials/python/) Related Learning Paths: - [Exceptions, Logging, and Debugging](https://realpython.com/learning-paths/exception-handling-logging-debugging/?utm_source=realpython&utm_medium=web&utm_campaign=related-learning-path&utm_content=python-exceptions) - [Revisit Python Fundamentals](https://realpython.com/learning-paths/python3-introduction/?utm_source=realpython&utm_medium=web&utm_campaign=related-learning-path&utm_content=python-exceptions) Related Courses: - [Raising and Handling Python Exceptions](https://realpython.com/courses/raising-handling-exceptions/?utm_source=realpython&utm_medium=web&utm_campaign=related-course&utm_content=python-exceptions) - [Introduction to Python Exceptions](https://realpython.com/courses/introduction-python-exceptions/?utm_source=realpython&utm_medium=web&utm_campaign=related-course&utm_content=python-exceptions) Related Tutorials: - [Defining Your Own Python Function](https://realpython.com/defining-your-own-python-function/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=python-exceptions) - [Reading and Writing Files in Python (Guide)](https://realpython.com/read-write-files-python/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=python-exceptions) - [Python's Built-in Exceptions: A Walkthrough With Examples](https://realpython.com/python-built-in-exceptions/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=python-exceptions) - [Object-Oriented Programming (OOP) in Python](https://realpython.com/python3-object-oriented-programming/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=python-exceptions) - [Dictionaries in Python](https://realpython.com/python-dicts/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=python-exceptions) ## Keep reading Real Python by creating a free account or signing in: [![Keep reading](https://realpython.com/static/videos/lesson-locked.f5105cfd26db.svg)](https://realpython.com/account/signup/?intent=continue_reading&utm_source=rp&utm_medium=web&utm_campaign=rwn&utm_content=v1&next=%2Fpython-exceptions%2F) [Continue »](https://realpython.com/account/signup/?intent=continue_reading&utm_source=rp&utm_medium=web&utm_campaign=rwn&utm_content=v1&next=%2Fpython-exceptions%2F) Already have an account? [Sign-In](https://realpython.com/account/login/?next=/python-exceptions/) Almost there! Complete this form and click the button below to gain instant access: × ![An Introduction to Python Exceptions](https://files.realpython.com/media/Python_Exceptions_Watermark.47f814fbeced.jpg) Python Exceptions: An Introduction (Sample Code) ##### Learn Python - [Start Here](https://realpython.com/start-here/) - [Learning Resources](https://realpython.com/search) - [Code Mentor](https://realpython.com/mentor/) - [Python Reference](https://realpython.com/ref/) - [Python Cheat Sheet](https://realpython.com/cheatsheets/python/) - [Support Center](https://support.realpython.com/) ##### Courses & Paths - [Learning Paths](https://realpython.com/learning-paths/) - [Quizzes & Exercises](https://realpython.com/quizzes/) - [Browse Topics](https://realpython.com/tutorials/all/) - [Live Courses](https://realpython.com/live/) - [Books](https://realpython.com/books/) ##### Community - [Podcast](https://realpython.com/podcasts/rpp/) - [Newsletter](https://realpython.com/newsletter/) - [Community Chat](https://realpython.com/community/) - [Office Hours](https://realpython.com/office-hours/) - [Learner Stories](https://realpython.com/learner-stories/) ##### Membership - [Plans & Pricing](https://realpython.com/account/join/) - [Team Plans](https://realpython.com/account/join-team/) - [For Business](https://realpython.com/account/join-team/inquiry/) - [For Schools](https://realpython.com/account/join-team/education-inquiry/) - [Reviews](https://realpython.com/learner-stories/) ##### Company - [About Us](https://realpython.com/about/) - [Team](https://realpython.com/team/) - [Mission & Values](https://realpython.com/mission/) - [Editorial Guidelines](https://realpython.com/editorial-guidelines/) - [Sponsorships](https://realpython.com/sponsorships/) - [Careers](https://realpython.workable.com/) - [Press Kit](https://realpython.com/media-kit/) - [Merch](https://realpython.com/merch) [Privacy Policy](https://realpython.com/privacy-policy/) ⋅ [Terms of Use](https://realpython.com/terms/) ⋅ [Security](https://realpython.com/security/) ⋅ [Contact](https://realpython.com/contact/) Happy Pythoning\! © 2012–2026 DevCademy Media Inc. DBA Real Python. All rights reserved. REALPYTHONℱ is a trademark of DevCademy Media Inc. [![Real Python - Online Python Training (logo)](https://realpython.com/static/real-python-logo-primary.973743b6d39d.svg)](https://realpython.com/) ![](https://www.facebook.com/tr?id=2220911568135371&ev=PageView&noscript=1) You've blocked notifications
Readable Markdown
by [Said van de Klundert](https://realpython.com/python-exceptions/#author) Reading time estimate 24m [basics](https://realpython.com/tutorials/basics/) [python](https://realpython.com/tutorials/python/) Python exceptions provide a mechanism for handling errors that occur during the execution of a program. Unlike syntax errors, which are detected by the parser, Python raises exceptions when an error occurs in syntactically correct code. Knowing how to raise, catch, and handle exceptions effectively helps to ensure your program behaves as expected, even when encountering errors. **By the end of this tutorial, you’ll understand that:** - 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. - Combining `try`, `except`, and `pass` allows your program to **continue silently** without handling the exception. In this tutorial, you’ll get to know Python exceptions and all relevant keywords for exception handling by walking through a practical example of handling a platform-related exception. Finally, you’ll also learn how to create your own custom Python exceptions. ***Take the Quiz:*** Test your knowledge with our interactive “Python Exceptions: An Introduction” quiz. You’ll receive a score upon completion to help you track your learning progress: *** [![An Introduction to Python Exceptions](https://files.realpython.com/media/Python_Exceptions_Watermark.47f814fbeced.jpg)](https://realpython.com/quizzes/python-exceptions/) **Interactive Quiz** [Python Exceptions: An Introduction](https://realpython.com/quizzes/python-exceptions/) In this quiz, you'll test your understanding of Python exceptions. You'll cover the difference between syntax errors and exceptions and learn how to raise exceptions, make assertions, and use the try and except block. ## Understanding Exceptions and Syntax Errors [Syntax errors](https://realpython.com/invalid-syntax-python/) occur when the parser detects an incorrect statement. Observe the following example: The arrow indicates where the parser ran into the **syntax error**. Additionally, the error message gives you a hint about what went wrong. In this example, there was one bracket too many. Remove it and run your code again: This time, you ran into an **exception error**. This type of error occurs whenever syntactically correct Python code results in an error. The last line of the message indicates what type of exception error you ran into. Instead of just writing *exception error*, Python details what *type* of exception error it encountered. In this case, it was a `ZeroDivisionError`. Python comes with [various built-in exceptions](https://docs.python.org/3/library/exceptions.html) as well as the possibility to create user-defined exceptions. ## Raising an Exception in Python There are scenarios where you might want to stop your program by raising an exception if a condition occurs. You can do this with the [`raise`](https://realpython.com/python-raise-exception/) keyword: [![Illustration of raise statement usage](https://files.realpython.com/media/raise.3931e8819e08.png)](https://files.realpython.com/media/raise.3931e8819e08.png) You can even complement the statement with a custom message. Assume that you’re writing a tiny toy program that expects only numbers up to `5`. You can raise an error when an unwanted condition occurs: In this example, you raised an `Exception` object and passed it an informative custom message. You built the message using an [f-string](https://realpython.com/python-f-strings/) and a [self-documenting expression](https://realpython.com/python-f-strings/#self-documenting-expressions-for-debugging). When you run `low.py`, you’ll get the following output: The program comes to a halt and displays the exception to your [terminal](https://realpython.com/terminal-commands/) or [REPL](https://realpython.com/python-repl/), offering you helpful clues about what went wrong. Note that the final call to [`print()`](https://realpython.com/python-print/) never executed, because Python raised the exception before it got to that line of code. With the `raise` keyword, you can raise any exception object in Python and stop your program when an unwanted condition occurs. ## Debugging During Development With `assert` Before moving on to the most common way of working with exceptions in Python using [the `try` 
 `except` block](https://realpython.com/python-exceptions/#handling-exceptions-with-the-try-and-except-block), you’ll take a quick look at an exception that’s a bit different than the others. Python offers a specific exception type that you should only use when debugging your program during development. This exception is the `AssertionError`. The `AssertionError` is special because you shouldn’t ever raise it yourself using `raise`. Instead, you use [the `assert` keyword](https://dbader.org/blog/python-assert-tutorial) to check whether a condition is met and let Python raise the `AssertionError` if the condition isn’t met. The idea of an assertion is that your program should only attempt to run if certain conditions are in place. If Python checks your assertion and finds that the condition is `True`, then that is excellent! The program can continue. If the condition turns out to be `False`, then your program raises an `AssertionError` exception and stops right away: [![Python assert statement](https://files.realpython.com/media/assert.f6d344f0c0b4.png)](https://files.realpython.com/media/assert.f6d344f0c0b4.png) Revisit your tiny script, `low.py`, from [the previous section](https://realpython.com/python-exceptions/#raising-an-exception-in-python). Currently, you’re explicitly raising an exception when a certain condition isn’t met: Assuming that you’ll handle this constraint safely for your production system, you could replace this [conditional statement](https://realpython.com/python-conditional-statements/) with an assertion for a quick way to retain this sanity check during development: If the `number` in your program is below `5`, then the assertion passes and your script continues with the next line of code. However, if you set `number` to a value higher than `5`—for example, `10`—then the outcome of the assertion will be `False`: In that case, Python raises an `AssertionError` that includes the message you passed, and ends the program execution: In this example, raising an `AssertionError` exception is the last thing that the program will do. The program will then come to halt and won’t continue. The call to `print()` that follows the assertion won’t execute. Using assertions in this way can be helpful when you’re debugging your program during development because it can be quite a fast and straightforward to add assertions into your code. However, you shouldn’t rely on assertions for catching crucial run conditions of your program in production. That’s because Python globally disables assertions when you run it in optimized mode using the [`-O` and `-OO` command line options](https://docs.python.org/3/using/cmdline.html#cmdoption-O): In this run of your program, you used the `-O` command line option, which removes all `assert` statements. Therefore, your script ran all the way to the end and displayed a number that is *dreadfully* high\! In production, your Python code may run using this optimized mode, which means that assertions aren’t a reliable way to handle runtime errors in production code. They can be quick and useful helpers when your debugging your code, but you should never use assertions to set crucial constraints for your program. If `low.py` should reliably fail when `number` is above `5`, then it’s best to stick with [raising an exception](https://realpython.com/python-exceptions/#raising-an-exception-in-python). However, sometimes you might not want your program to fail when it encounters an exception, so how should you handle those situations? ## Handling Exceptions With the `try` and `except` Block In Python, you use the `try` and `except` block to catch and handle exceptions. Python executes code following the `try` statement as a normal part of the program. The code that follows the `except` statement is the program’s response to any exceptions in the preceding `try` clause: [![Diagram showing try and except statements](https://files.realpython.com/media/try_except.c94eabed2c59.png)](https://files.realpython.com/media/try_except.c94eabed2c59.png) As you saw earlier, when syntactically correct code runs into an error, Python will raise an exception error. This exception error will crash the program if you don’t handle it. In the `except` clause, you can determine how your program should respond to exceptions. The following function can help you understand the `try` and `except` block: The `linux_interaction()` can only run on a Linux system. Python will raise a `RuntimeError` exception if you call it on an operating system other then Linux. You can give the function a `try` by adding the following code: The way you handled the error here is by handing out a `pass`. If you run this code on a macOS or Windows machine, then you get the following output: You got nothing in response. The good thing here is that your program didn’t crash. But letting an exception that occurred pass silently is bad practice. You should always at least know about and [log](https://realpython.com/python-logging/) if some type of exception occurred when you ran your code. To this end, you can change `pass` into something that generates an informative message: When you now execute this code on a macOS or Windows machine, you’ll see the message from your `except` block printed to the console: When an exception occurs in a program that runs this function, then the program will continue as well as inform you about the fact that the function call wasn’t successful. What you didn’t get to see was the type of error that Python raised as a result of the function call. In order to see exactly what went wrong, you’d need to catch the error that the function raised. The following code is an example where you capture the `RuntimeError` and output that message to your screen: In the `except` clause, you assign the `RuntimeError` to the temporary variable `error`—often also called `err`—so that you can access the exception object in the indented block. In this case, you’re printing the object’s string representation, which corresponds to the error message attached to the object. Running this function on a macOS or Windows machine outputs the following: The first message is the `RuntimeError`, informing you that Python can only execute the function on a Linux machine. The second message tells you which function wasn’t executed. In the example above, you called a function that you wrote yourself. When you executed the function, you caught the `RuntimeError` exception and printed it to your screen. Here’s another example where you open a file and use a built-in exception: If `file.log` doesn’t exist, then this block of code will output the following: This is an informative message, and your program will still continue to run. However, your `except` block will currently catch *any* exception, whether that’s related to not being able to open the file or not. You could lead yourself onto a confusing path if you see this message even when Python raises a completely unrelated exception. Therefore, it’s always best to be *specific* when you’re handling an exception. In the [Python docs](https://docs.python.org/3/library/exceptions.html), you can see that there are a couple of built-in exceptions that you could raise in such a situation, for example: > *exception* `FileNotFoundError` > > Raised when a file or directory is requested but doesn’t exist. Corresponds to errno ENOENT. ([Source](https://docs.python.org/3/library/exceptions.html#FileNotFoundError)) You want to handle the situation when Python can’t find the requested file. To catch this type of exception and print it to screen, you could use the following code: In this case, if `file.log` doesn’t exist, then the output will be the following: You can have more than one function call in your `try` clause and anticipate catching various exceptions. Something to note here is that the code in the `try` clause will stop as soon as it encounters any one exception. Look at the following code. Here, you first call `linux_interaction()` and then try to open a file: If you run this code on a macOS or Windows machine, then you’ll see the following: Inside the `try` clause, you ran into an exception immediately and didn’t get to the part where you attempt to open `file.log`. Now look at what happens when you run the code on a Linux machine if the file doesn’t exist: Note that if you’re handling specific exceptions as you did above, then the order of the `except` clauses doesn’t matter too much. It’s all about which of the exceptions Python raises first. As soon as Python raises an exception, it checks the except clauses from top to bottom and executes the first matching one that it finds. Here are the key takeaways about using Python’s `try` 
 `except` statements: - Python executes a `try` clause up until the point where it encounters the first exception. - Inside the `except` clause—the exception handler—you determine how the program responds to the exception. - You can anticipate [multiple exceptions](https://realpython.com/python-catch-multiple-exceptions/) and differentiate how the program should respond to them. - [Avoid using bare `except` clauses](https://realpython.com/the-most-diabolical-python-antipattern/), because they can hide unexpected exceptions. While using `try` together with `except` is probably the most common error handling that you’ll encounter, there’s more that you can do to fine-tune your program’s response to exceptions. ## Proceeding After a Successful Try With `else` You can use Python’s `else` statement to instruct a program to execute a certain block of code only in the absence of exceptions: [![Diagram of try, except, and else statements in Python](https://files.realpython.com/media/try_except_else.703aaeeb63d3.png)](https://files.realpython.com/media/try_except_else.703aaeeb63d3.png) Look at the following example: If you were to run this code on a Linux system, then the output would be the following: Because the program didn’t run into *any* exceptions, Python executed the code in the `else` clause. However, if you run this code on a macOS or Windows system, then you get a different output: The `linux_interaction()` function raised a `RuntimeError`. You’ve handled the exception, so your program doesn’t crash, and instead prints the exception message to the console. The code nested under the `else` clause, however, doesn’t execute, because Python encountered an exception during execution. Note that structuring your code like this is different from just adding the call to `print()` outside of the context of the `try` 
 `except` block: If you don’t nest the `print()` call under the `else` clause, then it’ll execute even if Python encounters the `RuntimeError` that you handle in the `except` block above. On a Linux system, the output would be the same, but on macOS or Windows, you’d get the following output: Nesting code under the `else` clause assures that it’ll only run when Python doesn’t encounter any exception when executing the `try` 
 `except` block. You can also create a nested `try` 
 `except` block inside the `else` clause and catch possible exceptions there as well: If you were to execute this code on a Linux machine, then you’d get the following result: From the output, you can see that `linux_interaction()` ran. Because Python encountered no exceptions, it attempted to open `file.log`. That file didn’t exist, but instead of letting the program crash, you caught the `FileNotFoundError` exception and printed a message to the console. ## Cleaning Up After Execution With `finally` Imagine that you always had to implement some sort of action to clean up after executing your code. Python enables you to do so using the `finally` clause: [![Diagram explaining try except else finally statements](https://files.realpython.com/media/try_except_else_finally.a7fac6c36c55.png)](https://files.realpython.com/media/try_except_else_finally.a7fac6c36c55.png) Have a look at the following example: In this code, Python will execute everything in the `finally` clause. It doesn’t matter if you encounter an exception somewhere in any of the `try` 
 `except` blocks. Running the code on a macOS or Windows machine will output the following: Note that the code inside the `finally` block will execute regardless of whether or not you’re handling the exceptions: You simplified the example code from above, but `linux_interaction()` still raises an exception on a macOS or Windows system. If you now run this code on an operating system other than Linux, then you’ll get the following output: Despite the fact that Python raised the `RuntimeError`, the code in the `finally` clause still executed and printed the message to your console. This can be helpful because even code outside of a `try`
 `except` block won’t necessarily execute if your script encounters an unhandled exception. In that case, your program will terminate and the code *after* the `try` 
 `except` block will never run. However, Python will still execute the code inside of the `finally` clause. This helps you make sure that resources like [file handles](https://realpython.com/why-close-file-python/) and [database connections](https://realpython.com/python-sql-libraries/) are cleaned up properly. ## Creating Custom Exceptions in Python With the large number of built-in exceptions that Python offers, you’ll likely find a fitting type when deciding which exception to raise. However, sometimes your code won’t fit the mold. Python makes it straightforward to create custom exception types by inheriting from a built-in exception. Think back to your `linux_interaction()` function: Using a [`RuntimeError`](https://docs.python.org/3/library/exceptions.html#RuntimeError) isn’t a bad choice in this situation, but it would be nice if your exception name was a bit more specific. For this, you can create a custom exception: You generally create a custom exception in Python by inheriting from `Exception`, which is the base class for most built-in Python exceptions as well. You could also inherit from a different exception, but choosing `Exception` is usually the best choice. That’s really all that you need to do. In the code snippet above, you also added a [docstring](https://realpython.com/documenting-python-code/) that describes the exception type and serves as the class body. While you can customize your exception object, you don’t need to do that. It’s often enough to give your custom Python exceptions a descriptive name, so you’ll know what happened when Python raises this exception in your code. Now that you’ve defined the custom exception, you can raise it like any other Python exception: If you now call `linux_interaction()` on macOS or Windows, then you’ll see that Python raises your custom exception: You could even use your custom `PlatformException` as a parent class for other custom exceptions that you could descriptively name for each of the platforms that users may run your code on. ## Conclusion At this point, you’re familiar with the basics of using Python exceptions. After seeing the difference between syntax errors and exceptions, you learned about various ways to raise, catch, and handle exceptions in Python. You also learned how you can create your own custom exceptions. In this article, you gained experience working with the following exception-related keywords: - `raise` allows you to raise an exception at any time. - `assert` enables you to verify if a certain condition is met and raises an exception if it isn’t. - In the `try` clause, all statements are executed until an exception is encountered. - `except` allows you to catch and handle the exception or exceptions that Python encountered in the `try` clause. - `else` lets you code sections that should run only when Python encounters no exceptions in the `try` clause. - `finally` enables you to execute sections of code that should always run, whether or not Python encountered any exceptions. You now understand the basic tools that Python offers for dealing with exceptions. If you’re curious about the topic and want to dive deeper, then take a look at the following tutorials: - [Python’s Built-in Exceptions: A Walkthrough With Examples](https://realpython.com/python-built-in-exceptions/) - [Exception Groups and `except*`](https://realpython.com/python311-exception-groups/#exception-groups-and-except-in-python-311) - [Python `raise`: Effectively Raising Exceptions in Your Code](https://realpython.com/python-raise-exception/) - [How to Catch Multiple Exception in Python](https://realpython.com/python-catch-multiple-exceptions/) - [Understanding the Python Traceback](https://realpython.com/python-traceback/) - [LBYL vs EAFP: Preventing or Handling Errors in Python](https://realpython.com/python-lbyl-vs-eafp/) What’s your favorite aspect of exception handling in Python? Share your thoughts in the comments below. ## Frequently Asked Questions Now that you have some experience with Python exceptions, you can use the questions and answers below to check your understanding and recap what you’ve learned. These FAQs are related to the most important concepts you’ve covered in this tutorial. Click the *Show/Hide* toggle beside each question to reveal the answer. Exceptions in Python are errors that occur during the execution of a program, disrupting the normal flow of the program. You handle exceptions in Python using a `try` 
 `except` block. Python executes the code in the `try` block and if an exception occurs, it switches to executing the code in the `except` block to handle the exception. However, only the exceptions that are explicitly specified in the `except` block will be handled. If an exception is not caught, it’ll propagate up the call stack and may result in the termination of your program. To catch all exceptions in Python, you can use a bare `except` clause or write `except Exception`, but it’s recommended to catch specific exceptions to avoid masking unexpected errors. In a `try` 
 `except` block, Python executes the code under `try` and if an exception occurs, it immediately jumps to the `except` block to handle it, allowing the program to continue running. Using `try` 
 `except` with `pass` allows the program to ignore the exception and continue execution without taking any specific action in response to the error. However, this practice can hide potential issues, making it harder to debug and maintain the code, so use it with caution. It’s generally better to either handle the exception explicitly or log it for debugging purposes. You raise an exception in Python using the `raise` keyword followed by an exception object, which can include a custom message. You can use the `assert` keyword to check if a condition is true during development. If the condition is false, it raises an `AssertionError`, which can help with debugging. Note that assertions can be disabled by running Python with the `-O` (optimize) flag. Therefore, you shouldn’t rely on assertions for critical checks in production code, as they may be ignored. The `finally` clause contains code that will always execute after a `try` block, regardless of whether an exception was raised or not, ensuring necessary cleanup actions occur. ***Take the Quiz:*** Test your knowledge with our interactive “Python Exceptions: An Introduction” quiz. You’ll receive a score upon completion to help you track your learning progress: *** [![An Introduction to Python Exceptions](https://files.realpython.com/media/Python_Exceptions_Watermark.47f814fbeced.jpg)](https://realpython.com/quizzes/python-exceptions/) **Interactive Quiz** [Python Exceptions: An Introduction](https://realpython.com/quizzes/python-exceptions/) In this quiz, you'll test your understanding of Python exceptions. You'll cover the difference between syntax errors and exceptions and learn how to raise exceptions, make assertions, and use the try and except block.
Shard71 (laksa)
Root Hash13351397557425671
Unparsed URLcom,realpython!/python-exceptions/ s443