User Defined Exception
User Defined Exception
Basic Syntax:
class CustomError(Exception):
"""Base class for other custom exceptions"""
pass
Example:
class DivisionByZeroError(Exception):
pass
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.
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