适用于 .NET 的 Microsoft 服务点 (POS for .NET) 错误处理通过使用异常实现。 四个 POS for .NET 异常类如下所示:
标准统一服务点 (UnifiedPOS) 错误代码由 ErrorCode 枚举表示。
PosException
PosException 是 PosControlException、PosManagementException 和 PosLibraryException 的基本异常类。 PosException 派生自 System.Exception。
PosControlException
PosControlException 是服务对象针对 POS for .NET 应用程序引发的标准异常。
PosManagementException
PosManagementException 由 POS for .NET 设备管理引发。 应用程序和服务对象不得引发 PosManagementException。
PosLibraryException
PosLibraryException 由 PosExplorer引发。 应用程序和服务对象不得引发 PosLibraryException。
错误代码
下表提供了 UnifiedPOS 标准错误代码与 POS for .NET 提供的 ErrorCode 值之间的映射。
POS ErrorCode 成员 | UnifiedPOS 错误代码 | 错误原因 |
---|---|---|
忙碌 | E_BUSY | 当前服务对象状态不允许此请求。 |
Claimed | E_CLAIMED | 另一个服务对象实例已声明 POS 设备。 |
已关闭 | E_CLOSED | POS 设备已关闭。 |
已放弃 | E_DEPRECATED | 该方法已弃用,不再可用。 |
已禁用 | E_DISABLED | 禁用设备时无法执行该操作。 |
Exists | E_EXISTS | 文件名或其他指定值已存在。 |
扩展 | E_EXTENDED | 出现特定于设备的错误条件。 |
失败 | E_FAILURE | POS 设备无法执行请求的过程,即使设备已连接到系统并处于活动状态也是如此。 |
Illegal | E_ILLEGAL | POS 应用程序尝试对设备执行非法或不受支持的操作,或者使用了无效参数值。 |
NoExist | E_NOEXIST | 文件名或其他指定值不存在。 |
NoHardware | E_NOHARDWARE | POS 设备未连接到系统或未打开。 |
NoService | E_NOSERVICE | 服务对象无法与设备通信,通常是由于设置或配置错误。 |
NotClaimed | E_NOTCLAIMED | POS 应用程序尝试访问必须声明的独占用途设备,然后才能使用方法或属性集操作。 |
Offline | E_OFFLINE | POS 设备处于脱机状态。 |
超时 | E_TIMEOUT | 服务对象在等待 POS 设备响应时超时。 |
示例
下面的代码示例演示 MSR 如何处理 POS 异常,并使用这些异常中包含的 ErrorCodes 来收集有关这些异常的信息。
// Create a new instance of the MSR and opens the device.
msr = (Msr)explorer.CreateInstance(msrinfo);
msr.Open();
// Try to enable the device without first claiming it.
// This will throw a PosControlException which, through
// its ErrorCode, will yield information about the exception.
try
{
msr.DeviceEnabled = true;
}
catch (PosControlException e)
{
// Examine the ErrorCode to determine the cause of the error.
if (e.ErrorCode == ErrorCode.NoHardware)
{
Console.WriteLine("The POS device is not connected ");
Console.WriteLine("to the system or is not turned on.");
}
if (e.ErrorCode == ErrorCode.Timeout)
{
Console.WriteLine("The Service Object timed out
waiting for a response from the POS device.");
}
// The example has not claimed the MSR, which is an
// exclusive-access device, before trying to enable
// it. This will throw the PosControlException
// and trigger the following conditional block.
// Once triggered, the MSR will be claimed and enabled.
if (e.ErrorCode == ErrorCode.NotClaimed)
{
Console.WriteLine("The POS application attempted to access ");
Console.WriteLine("an exclusive-use device that must be ");
Console.WriteLine("claimed before the method or property ");
Console.WriteLine("set action can be used.")
msr.Claim(1000);
msr.DeviceEnabled = true;
}
}