Exceptions Python Module¶
Differences between V1.X.X and V2.X.X of the EzSnmp Exception Handling
The primary difference between V1.X.X and V2.X.X lies in how exceptions are structured and handled.
Base Exception Class:
V1.X.X: Uses
EzSNMPErroras the base exception class. Specific error types inherit from this class (e.g.,EzSNMPConnectionError,EzSNMPTimeoutError).V2.X.X: Uses
GenericErroras the base exception class. All other exceptions inherit fromGenericError.
Specific Exception Classes:
V1.X.X: Defines individual exception classes for various EzSnmp errors, such as
EzSNMPConnectionError,EzSNMPTimeoutError,EzSNMPUnknownObjectIDError,EzSNMPNoSuchNameError,EzSNMPNoSuchObjectError,EzSNMPNoSuchInstanceError, andEzSNMPUndeterminedTypeError.V2.X.X: While retaining many of the same error types (
ConnectionError,NoSuchInstanceError,NoSuchNameError,NoSuchObjectError,TimeoutError,UndeterminedTypeError,UnknownObjectIDError), V2.X.X adds new ones related to packet and parsing errors:PacketErrorandParseError. Crucially, all inherit fromGenericError.
Error Handling:
V1.X.X: Implicit exception handling. The code relies on catching specific exception types.
V2.X.X: Introduces
_handle_error(e). This function maps exceptions from the lower-level C++ net-snmp wrapper (e.g.,ConnectionErrorBase) to the corresponding Python exception classes. This provides a cleaner Python interface, abstracting C++ error types.
In summary, V2.X.X offers a more robust and structured approach. _handle_error for mapping C++ exceptions
improves usability and maintainability. The common base class (GenericError) and more specific exception
types facilitate organized error handling. The addition of PacketError and ParseError enhances error
reporting.
- class ezsnmp.exceptions.ConnectionError(message)¶
Bases:
GenericErrorException raised for SNMP connection errors.
- Parameters:
message (str) – A descriptive error message.
- __init__(message)¶
- class ezsnmp.exceptions.GenericError(message)¶
Bases:
ExceptionException raised for general SNMP errors in the ezsnmp library.
- Parameters:
message (str) – Explanation of the error.
- __init__(message)¶
- class ezsnmp.exceptions.NoSuchInstanceError(message)¶
Bases:
GenericErrorException raised when an SNMP operation encounters a “No Such Instance” error.
This indicates that the requested object instance does not exist on the agent.
- Parameters:
message (str) – Explanation of the error.
- __init__(message)¶
- class ezsnmp.exceptions.NoSuchNameError(message)¶
Bases:
GenericErrorException raised when an SNMP operation encounters a “No Such Name” error.
This indicates that the requested object name does not exist on the agent.
- Parameters:
message (str) – Explanation of the error.
- __init__(message)¶
- class ezsnmp.exceptions.NoSuchObjectError(message)¶
Bases:
GenericErrorException raised when an SNMP operation encounters a “No Such Object” error.
- Parameters:
message (str) – Explanation of the error.
- __init__(message)¶
- class ezsnmp.exceptions.PacketError(message)¶
Bases:
GenericErrorException raised when an error occurs related to SNMP packet processing.
- Parameters:
message (str) – Explanation of the error.
- __init__(message)¶
- class ezsnmp.exceptions.ParseError(message)¶
Bases:
GenericErrorException raised when an error occurs while parsing SNMP messages or arguments.
- Parameters:
message (str) – Explanation of the error.
- __init__(message)¶
- class ezsnmp.exceptions.TimeoutError(message)¶
Bases:
GenericErrorException raised when an SNMP operation times out.
- Parameters:
message (str) – A descriptive message about the timeout error.
- __init__(message)¶
- class ezsnmp.exceptions.UndeterminedTypeError(message)¶
Bases:
GenericErrorException raised when an SNMP type cannot be determined.
- Parameters:
message (str) – A descriptive error message.
- __init__(message)¶
- class ezsnmp.exceptions.UnknownObjectIDError(message)¶
Bases:
GenericErrorException raised when an unknown SNMP Object ID is encountered.
- Parameters:
message (str) – A string containing the error message.
- __init__(message)¶
- ezsnmp.exceptions._handle_error(e)¶
Handle and map C++ error types to corresponding Python exceptions.
This function inspects the type of the given exception e and raises a corresponding Python exception that can be caught. It maps various C++ error base types to custom Python exceptions.
- Parameters:
e (Exception) – The exception object to be handled and mapped.
- Raises:
ConnectionError – If the exception type is ConnectionErrorBase.
GenericError – If the exception type is GenericErrorBase.
NoSuchInstanceError – If the exception type is NoSuchInstanceErrorBase.
NoSuchNameError – If the exception type is NoSuchNameErrorBase.
NoSuchObjectError – If the exception type is NoSuchObjectErrorBase.
PacketError – If the exception type is PacketErrorBase.
ParseError – If the exception type is ParseErrorBase.
TimeoutError – If the exception type is TimeoutErrorBase.
UndeterminedTypeError – If the exception type is UndeterminedTypeErrorBase.
UnknownObjectIDError – If the exception type is UnknownObjectIDErrorBase.
Exception – If the exception type does not match any of the above, the original exception e is raised.