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
EzSNMPError
as the base exception class. Specific error types inherit from this class (e.g.,EzSNMPConnectionError
,EzSNMPTimeoutError
).V2.X.X: Uses
GenericError
as 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:PacketError
andParseError
. 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:
GenericError
- __init__(message)¶
Exception raised for SNMP connection errors.
This class represents an error that occurs during an SNMP connection attempt.
- Parameters:
message (str) – A descriptive error message.
- class ezsnmp.exceptions.GenericError(message)¶
Bases:
Exception
- __init__(message)¶
Exception raised for handling SNMP errors in the ezsnmp library.
This class extends the standard Exception class to provide detailed error messages specific to SNMP operations.
- Parameters:
message (str) – Explanation of the error.
- class ezsnmp.exceptions.NoSuchInstanceError(message)¶
Bases:
GenericError
- __init__(message)¶
Exception raised for handling SNMP “No Such Instance” errors.
This exception is thrown when an SNMP operation encounters a “No Such Instance” error, indicating that the requested instance does not exist.
- Parameters:
message (str) – Explanation of the error.
- class ezsnmp.exceptions.NoSuchNameError(message)¶
Bases:
GenericError
- __init__(message)¶
Exception raised for handling SNMP “No Such Name” errors.
This class represents an error that occurs when an SNMP operation encounters a “No Such Name” error, indicating that the requested object does not exist.
- Parameters:
message (str) – Explanation of the error.
- class ezsnmp.exceptions.NoSuchObjectError(message)¶
Bases:
GenericError
- __init__(message)¶
Exception raised for handling SNMP “No Such Object” errors.
This exception is thrown when an SNMP operation encounters a “No Such Object” error.
- Parameters:
message (str) – Explanation of the error.
- class ezsnmp.exceptions.PacketError(message)¶
Bases:
GenericError
- __init__(message)¶
Exception raised for handling SNMP packet errors.
This exception is thrown when an error occurs related to SNMP packet processing.
- Parameters:
message (str) – Explanation of the error.
- class ezsnmp.exceptions.ParseError(message)¶
Bases:
GenericError
- __init__(message)¶
Exception raised for handling SNMP parse errors.
This exception is thrown when an error occurs while parsing SNMP command line arguments.
- Parameters:
message (str) – Explanation of the error.
- class ezsnmp.exceptions.TimeoutError(message)¶
Bases:
GenericError
- __init__(message)¶
Exception raised for handling SNMP timeout errors.
This class represents an error that occurs when an SNMP operation times out.
- Parameters:
message (str) – A descriptive message about the timeout error.
- class ezsnmp.exceptions.UndeterminedTypeError(message)¶
Bases:
GenericError
- __init__(message)¶
Exception raised for undetermined SNMP type errors.
This exception is thrown when an SNMP type cannot be determined.
- Parameters:
message (str) – A descriptive error message.
- class ezsnmp.exceptions.UnknownObjectIDError(message)¶
Bases:
GenericError
- __init__(message)¶
Exception raised for unknown SNMP Object ID errors.
This exception is thrown when an unknown SNMP Object ID is encountered.
- Parameters:
message (str) – A string containing the error 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.