0% found this document useful (0 votes)
6 views1 page

User Defined Exception

User-defined exceptions are custom exceptions created to handle specific error conditions not covered by Python's built-in exceptions, enhancing code readability and maintainability. The document provides a basic syntax for defining a custom exception and an example of a DivisionByZeroError. It also lists several programs that can be implemented using user-defined exceptions, such as age validation and password validation.

Uploaded by

www.ananya5002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

User Defined Exception

User-defined exceptions are custom exceptions created to handle specific error conditions not covered by Python's built-in exceptions, enhancing code readability and maintainability. The document provides a basic syntax for defining a custom exception and an example of a DivisionByZeroError. It also lists several programs that can be implemented using user-defined exceptions, such as age validation and password validation.

Uploaded by

www.ananya5002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

User-defined exceptions are custom exceptions that you create to handle specific error conditions in your application

that aren't covered by Python's built-in exceptions.


Why Use User-Defined Exceptions?
1. To create application-specific error types
2. To provide more meaningful error messages
3. To handle specific error conditions in your domain
4. To make your code more readable and maintainable

Basic Syntax:

class CustomError(Exception):
"""Base class for other custom exceptions"""
pass

Example:

Division by Zero (Custom)

class DivisionByZeroError(Exception):
pass

def divide(a, b):


if b == 0:
raise DivisionByZeroError("Cannot divide by zero")
return a / b

Explanation
class DivByZeroError(Exception):
 This line defines a custom exception class called NegativeSalaryError.
 It inherits from Python’s built-in Exception class, meaning it behaves like any other exception (e.g.,
ValueError, TypeError) but with a custom name and purpose.
 pass:
It simply means “do nothing.” We don't need to add anything special to the class right now. The base
Exception behavior is enough.

raise DivisionByZeroError("Cannot divide by zero")

 If the condition is true (i.e., salary is negative), this line raises the custom exception NegativeSalaryError.
 The message "Salary cannot be negative" is passed to the exception and will be shown when the
exception is caught or printed.

Programs to be implemented:
1. Age Validation
2. Password Validation (Length, numeric and special characters)
3. Invalid Email Format
4. Invalid Phone Number
5. File Extension Check
6. Invalid Input Type
7. Invalid PIN Code

You might also like