0% found this document useful (0 votes)
4 views3 pages

Exception Handling in Python Part2

The document explains how to create and raise custom exceptions in Python using the `raise` statement, which interrupts normal program flow to signal error conditions. It also discusses the `assert` statement as a debugging tool that checks expressions and raises `AssertionError` if conditions are false, emphasizing that assertions are meant for development and can be disabled in optimized code. The document advises using exceptions for proper error handling in production rather than relying on assertions.

Uploaded by

Amandeep kaur
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Exception Handling in Python Part2

The document explains how to create and raise custom exceptions in Python using the `raise` statement, which interrupts normal program flow to signal error conditions. It also discusses the `assert` statement as a debugging tool that checks expressions and raises `AssertionError` if conditions are false, emphasizing that assertions are meant for development and can be disabled in optimized code. The document advises using exceptions for proper error handling in production rather than relying on assertions.

Uploaded by

Amandeep kaur
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

7.

2 Creating and Raising a Custom Exception:

class CustomError(Exception):
pass
try:
raise CustomError("This is a custom exception")
except CustomError as e:
print(f"Custom Error: {e}")

Here, a custom exception `CustomError` is defined, and then it's raised in the
`try` block with a custom message.

Key Points:

• The `raise` statement interrupts the normal flow of the program and raises
an exception.
• You can raise built-in exceptions or create your own custom exceptions
by defining a new class that inherits from the `Exception` class (or its
subclasses).
• The `raise` statement can include an optional custom message to provide
additional information about the reason for raising the exception.

Using `raise` is helpful when you want to handle exceptional cases that are not
automatically detected by the interpreter. It allows you to communicate specific
error conditions and take appropriate actions in response to those conditions.

9. assert:

In Python, the `assert` statement is used as a debugging aid. It checks whether a


given expression is true and raises an `AssertionError` exception if the
expression is false. The `assert` statement is typically used to verify that certain
conditions hold true during the execution of a program. It is important to note
that assertions are meant for debugging purposes and can be disabled globally in
a Python interpreter for optimized code execution.

Syntax:

assert expression, "Optional error message"

• `expression`: The condition that the interpreter checks. If it evaluates to


`False`, an `AssertionError` is raised.
• `"Optional error message"`: A custom error message that provides
additional information about the failed assertion.

Example:

def divide(a, b):


assert b != 0, "Cannot divide by zero"
return a / b

try:
result = divide(10, 0)
except AssertionError as e:
print(f"Assertion Error: {e}")

In this example, the `divide` function uses an `assert` statement to check


whether the divisor `b` is not zero before performing the division. If the
assertion fails (i.e., if `b` is zero), an `AssertionError` is raised with the
specified error message.
Key Points:

• The `assert` statement is a debugging tool and should not be used for
handling runtime errors in production code.
• If Python is run with the `-O` (optimize) command-line switch, assertions
are ignored, and the corresponding code is not executed.
• It is advisable to use `assert` for checking conditions that should always
be true, indicating a bug in the program if they are false.

While assertions can be helpful for catching programming errors during


development, they are not a substitute for proper error handling in production
code. In production, it's better to use exceptions and appropriate error-handling
mechanisms to handle unexpected situations gracefully.

You might also like