来自我的代码的“抛出警告" [英] 'Throwing warnings' from my code

查看:27
本文介绍了来自我的代码的“抛出警告"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个处理一些文件的工具.此工具将具有命令行界面,但也可用作第三方代码的类库.为了处理错误,我只是抛出一个异常.第三方代码可以处理异常,命令行界面可能只是打印它并中止.但是,除了致命错误,也有可能出现不致命的情况,过程可以继续,对此我想抛出警告"并继续.

I am writing a tool that processes some files. This tool will have a command-line interface but can also be used as a class library from third-party code. To deal with an error, I just throw an exception. Third-party code can handle the exception, and the command-line interface might just print it and abort. However, apart from fatal errors, it is also possible that a situation arises that is not fatal and the process can continue, for which I would like to 'throw a warning' and continue.

我如何处理警告,这样第三方代码和命令行界面都可以用它做一些事情?

How do I deal with warnings, in such way that both third-party code and the command-line interface can do something with it?

推荐答案

您也为此抛出异常(假设错误条件确实是异常的 - 预计这种情况很少见).

You throw an exception for this as well (assuming the error condition is indeed exceptional - that is expected to be rare).

异常并不总是致命的 - 您需要抛出特定的类型异常,第三方代码和命令行代码可以捕获并采取行动.

Exceptions are not always lethal - you need to throw a specific type of exception that the third party code and command line code can catch and act on.

异常处理的一个规则是处理你知道如何处理的异常 - 如果你有一个特定的错误异常类型并且你记录了它,客户端代码应该知道如何处理它(或不知道).

One rule of exception handling is to handle exceptions you know how to handle - if you have a specific exception type for the error and you document it, client code should know how to deal with it (or not).

如果您尝试提供信息以使用代码 - 库中已经处理但用户可能想知道的警告 - 您可以使用 跟踪 子系统.所有用户需要做的就是配置一个监听器,并且能够在运行时获取信息.

If what you are trying to do is provide information to using code - warnings that are handled within your library already but that the users may want to know - you can output traces using the tracing subsystem. All users will need to do is configure a listener and will be able to get the information during runtime.

这篇关于来自我的代码的“抛出警告"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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