为许多不同的错误代码创建异常类 [英] Creating exception classes for lot different error codes

查看:85
本文介绍了为许多不同的错误代码创建异常类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为映射程序MapInfo Pro编写一个自定义.Net SDK,mapinfo仅公开了两种在COM上使用的方法 void Do(string cmd) string Eval(string cmd)。当您使用这些方法之一并且执行某些操作时,它将返回带有错误消息的COMException,并且我从MapInfo获取错误代码。因此,例如,错误消息如下所示:

I am writing a custom .Net SDK for the mapping program MapInfo Pro, mapinfo only exposes two methods to use over COM void Do(string cmd) and string Eval(string cmd). When you use one of these methods and you do something invaild, it returns a a COMException with a error message and I get the error code from MapInfo. So for example an error message looks like this:

 418  "Table not found."

现在,我想抛出某种意义的完全异常,而不是带有数字和消息,人们必须抓住,然后使用 if 块检查什么错误,他们说他们只是想抓住并做一些有意义的事情。像 TableNotFoundException 之类的东西。

Now I would like to throw some kind of meaning full exception rather then just a exception with a number and message that people have to catch then use a if block to check what error, something they say they only want to catch and do something meaning full with. Something like TableNotFoundException.

最大的问题是目前有 1999 可能的错误消息,现在我不希望所有这些错误都具有异常类。

The biggest problem is there are currently 1999 possible error messages, now I don't want a exception class for everyone of those errors.

SDK的思想是人们使用我的对象和方法而不是 Do Eval (如果愿意,他们可以使用它们)。

The idea of the SDK is that people use my objects and methods rather then Do and Eval(they can stil use them if they want).

我应该


  • 在我的SDK中我称为 Do Eval 使用try catch,然后检查错误代码,并从那里抛出更多有意义的内容,因为我知道我应该得到什么错误。

  • everywhere in my SDK that I call Do and Eval use a try catch and then check the error code and throw something more meaning full from there, because I know what errors I should get.


  • 我在其中有某种上帝的查询清单 Do Eval 抛出 COMException 时查找错误代码并从那里抛出有意义的错误?意味着我不必在try catch块中包装对 Do Eval 的每次调用(有很多

  • I have somekind of God lookup list in the Do and Eval that when a COMException is thrown it lookups the error code and throws the meaningfull error from there? Meaning I don't have to wrap every call to Do and Eval in try catch blocks(there are a lot of calls)

需要解释一些棘手的事情,希望大家能理解。

Bit of a tricky thing to explain so I hope people can understand it.

谢谢。

推荐答案

将调用包裹为 Do Eval 在自己的函数中捕获MapInfo错误。

Wrap the calls to Do and Eval in your own function which catches MapInfo errors.

由于错误类型太多,您可以创建一个具有更多描述性名称的枚举或将错误号映射为更友好名称的字典(如果原始名称不够好)-您不希望使用1999年的异常类,因此我建议仅使用一个包含以下内容的异常类错误编号和描述。您是说用户可以直接使用 Do Eval ,因此他们应该知道如何处理这些错误号,

As there are so many error types, you could create an enum with some more descriptive names or a dictionary mapping error numbers to more friendly names (if the original ones aren't good enough) - you don't want 1999 exception classes, so I'd suggest using only one exception class that contains the error number and description. You're saying that users could use Do and Eval directly, so they should know how to handle these error numbers, anyway.

或者,您可以创建一些分层的异常类型并决定要抛出哪种异常类型(需要字典映射0-1999 =>匹配的异常类型)-这样,用户可以更具体地确定要捕获的错误类型。

Alternatively, you could create a few hierarchical exception types and decide which one to throw (needs something a dictionary mapping 0-1999 => matching exception type) - that way, users can be a bit more specific about which types of errors to catch.

这篇关于为许多不同的错误代码创建异常类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆