Python Exception


In Python, exceptions are a way to handle errors and exceptional situations that may occur during the execution of a program. When an error occurs, an exception is raised, and if it is not handled, it can cause the program to terminate abruptly.

Exception handling allows you to catch and handle these exceptions gracefully, providing a way to recover from errors or take appropriate actions.

You can also include an optional else block that executes if no exception is raised inside the try block, and a finally block that always executes regardless of whether an exception occurred or not.

The finally block is often used for releasing resources or performing cleanup operations, such as closing files or database connections.


Python provides a variety of built-in exceptions that cover different types of errors, such as ValueError, TypeError, FileNotFoundError, etc.

Additionally, you can create custom exception classes by deriving them from the base Exception class.

To handle multiple exceptions, you can use multiple except blocks or catch multiple exceptions in a single block using parentheses:



In the example above, the code inside the try block attempts to perform a division by zero, which raises a ZeroDivisionError exception. The except block following it catches the exception and executes the code inside it. In this case, it prints an error message indicating a division by zero occurred.

Exception handling in Python allows you to gracefully handle errors, provide informative error messages, and take appropriate actions based on different exceptional scenarios, enhancing the robustness and reliability of your programs.

Python provides several built-in exception types for different error scenarios.

Some commonly used built-in exceptions include:

ZeroDivisionError: Raised when attempting to divide by zero. TypeError: Raised when an operation or function is applied to an object of an inappropriate type.

ValueError: Raised when a built-in operation or function receives an argument of the correct type but an inappropriate value.

FileNotFoundError: Raised when a file or directory is requested but cannot be found. IndexError: Raised when a sequence subscript is out of range.

KeyError: Raised when a dictionary key is not found. ImportError: Raised when an import statement fails. Here is an example of exception handling in Python:


In this example, we are using the try block to try to convert the user's input to an integer and then divide it by 10. If the user enters a valid number, the try block will not raise an exception and the else block will be executed.

If the user enters an invalid number, the ValueError exception will be raised and the except ValueError block will be executed.

If the user enters a number of 0, the ZeroDivisionError exception will be raised and the except ZeroDivisionError block will be executed.

In both cases, the finally block will be executed before the program terminates. The try block is used to contain code that may raise an exception.

The except block is used to handle the exception. The else block is executed if no exception is raised. The finally block is executed regardless of whether or not an exception is raised.

Enroll Now

  • Python Programming
  • Machine Learning