Featured Article

"Mastering Python: Essential Exceptions Examples & Error Handling"

Christopher Jun 29, 2026

Understanding and Handling Python Exceptions

a poster with the words exception and an image of a bar graph on top of it
a poster with the words exception and an image of a bar graph on top of it

Python's exception handling mechanism is a powerful tool that allows us to control the normal flow of our programs and deal with unexpected situations. Exceptions in Python are raised when an error occurs, allowing us to catch and handle these errors gracefully. Let's dive into the world of Python exceptions with some practical examples.

Exception Handling in Python (with Example) - Scientech Easy
Exception Handling in Python (with Example) - Scientech Easy

What are Python Exceptions?

Exceptions in Python are events that occur during the execution of a program that disrupt the normal flow of the program's instructions. Python has a wide range of built-in exceptions that can be raised, such as NameError, TypeError, ValueError, and many more. We can also create our own custom exceptions.

Python Exception Handling Explained (Try-Except-Finally Guide)
Python Exception Handling Explained (Try-Except-Finally Guide)

Built-in Exceptions

Python's built-in exceptions are a part of the builtins module. Here are some of the most common ones:

Python custom exceptions cheat-sheet #tutorial #programming #python
Python custom exceptions cheat-sheet #tutorial #programming #python
  • NameError: Raised when a local or global name is not found.
  • TypeError: Raised when an operation or function is applied to an object of inappropriate type.
  • ValueError: Raised when a built-in operation or function receives an argument that has the right type but inappropriate value.
  • ZeroDivisionError: Raised when the second operator of a division or modulo operation is zero.

Handling Exceptions in Python

Python provides several ways to handle exceptions. The most common way is using the try and except blocks. Here's a simple example:

Python Try & Except
Python Try & Except

```python try: result = 10 / 0 except ZeroDivisionError: print("You cannot divide by zero!") ```

Catching Specific Exceptions

We can catch specific exceptions by mentioning the exception name in the except block. This allows us to handle different exceptions in different ways:

```python try: result = 10 / '2' except TypeError: print("You cannot divide an integer by a string!") except ZeroDivisionError: print("You cannot divide by zero!") ```

Catching Multiple Exceptions

Python Exception Handling Guide (2026)
Python Exception Handling Guide (2026)

We can also catch multiple exceptions in a single except block by separating them with a comma:

```python try: result = 10 / '2' except (TypeError, ZeroDivisionError): print("You cannot divide an integer by a string or divide by zero!") ```

Using else and finally Clauses

Python Errors Explained, a simple cheat sheet to look over and continue working.
Python Errors Explained, a simple cheat sheet to look over and continue working.
How to Print Exception in Python with Examples
How to Print Exception in Python with Examples
Python Errors — Types, Causes, Examples & Explanation
Python Errors — Types, Causes, Examples & Explanation
Handling Multiple Exceptions in Python - Scientech Easy
Handling Multiple Exceptions in Python - Scientech Easy
Python Error Handling: Catch Exceptions and Write Crash-Proof Code
Python Error Handling: Catch Exceptions and Write Crash-Proof Code
Python ValueError Exact Solution with Examples
Python ValueError Exact Solution with Examples
a screenshot of a black screen with text that reads,'information / error - unexpected
a screenshot of a black screen with text that reads,'information / error - unexpected
🐍 Python elif Statement Explained | Conditional Statements Made Easy
🐍 Python elif Statement Explained | Conditional Statements Made Easy
Python Conditional Statements & Loops
Python Conditional Statements & Loops
a poster with some text on it
a poster with some text on it
Python Execution
Python Execution
Python Mistakes Beginners Must Avoid | Python Basics Cheat Sheet Guide
Python Mistakes Beginners Must Avoid | Python Basics Cheat Sheet Guide
Common Python Errors (With Fixes) – Beginner’s Guide
Common Python Errors (With Fixes) – Beginner’s Guide
How (and Why) To Create Custom Exceptions in Python - KDnuggets
How (and Why) To Create Custom Exceptions in Python - KDnuggets
Try Except in Python - Scientech Easy
Try Except in Python - Scientech Easy
Ultimate Python Cheat Sheet for Beginner
Ultimate Python Cheat Sheet for Beginner
Deeply know errors and excepions in python
Deeply know errors and excepions in python
How to Use Python If Not Statement with Example
How to Use Python If Not Statement with Example
Enumeration in Python
Enumeration in Python
a screenshot of a computer screen with the text try and exexp in python
a screenshot of a computer screen with the text try and exexp in python

The else clause is executed if the try block doesn't raise an exception. The finally block is always executed, regardless of whether an exception was raised or not. Here's an example:

```python try: result = 10 / 2 except ZeroDivisionError: print("You cannot divide by zero!") else: print("No exceptions were raised.") finally: print("This block is always executed.") ```

Raising Exceptions

We can also raise exceptions using the raise keyword. This can be useful when we want to indicate that an error has occurred in our code. Here's an example:

```python if age < 18: raise ValueError("You must be at least 18 years old to vote.") ```

Conclusion

Python exceptions are a powerful tool that allows us to write robust and reliable code. By understanding how to handle and raise exceptions, we can control the flow of our programs and provide meaningful error messages to our users. Whether you're a seasoned Python developer or just starting out, understanding exceptions is a crucial part of becoming proficient in Python.