在UserControl中处理异常的正确方法

时间:2022-06-01 18:31:23

I'm build an UserControl and I'm not sure how to handle exceptions, the Control itself is not very complicated, the User chose an image from disk so they can authorize it, I don't know exactly how the control will be used so if I use a MessageBox I might block the application, and if I just re-throw it I might crash it.

我正在构建一个UserControl并且我不确定如何处理异常,Control本身并不是很复杂,用户从磁盘中选择了一个图像以便他们可以对其进行授权,我不确切知道如何使用该控件因此,如果我使用MessageBox,我可能会阻止该应用程序,如果我只是重新抛出它,我可能会崩溃它。

thanks in advance.

提前致谢。

Juan Zamudio

4 个解决方案

#1


3  

this is a common problem facing developers who build libraries. Try to weed out bugs and decide for the remaining error cases if it's an expected error (your control should not throw an exception but rather gracefully handle the error) or an unexpected exceptional condition (your control must throw an exception as soon as possible).

这是构建库的开发人员面临的常见问题。尝试清除错误并确定剩余的错误情况,如果它是预期的错误(您的控件不应抛出异常,而是优雅地处理错误)或意外的异常情况(您的控件必须尽快抛出异常)。

You might also have a look at Design By Contract, a methodology to declare required preconditions and guaranteed postconditions. This may sound academic, but it leads to more robust code.

您还可以查看Design By Contract,这是一种声明所需前置条件和保证后置条件的方法。这可能听起来很学术,但它会带来更强大的代码。

UPDATE: A good introduction is http://se.ethz.ch/~meyer/publications/computer/contract.pdf

更新:一个很好的介绍是http://se.ethz.ch/~meyer/publications/computer/contract.pdf

Regards, tamberg

#2


0  

unhandled exceptions should definitely be thrown so that the people using your control can see what's wrong.

绝对应该抛出未处理的异常,以便使用您的控件的人可以看到错误。

#3


0  

Only handle exceptions that you know of and know what to do with. Don't bother with generic handlers, like a MessageBox. Just let it propogate to the application where there's more context for diagnosing the error. It's their responsibility to catch any exceptions so that the application doesn't crash. In the case of an exception they can't do anything about because it has to do with the control's internals, you should handle that yourself and if it's partially the user's fault, wrap the exception with a message saying what is missing, with the original exception available via the InnerException property.

只处理您知道的并知道如何处理的异常。不要使用通用处理程序,如MessageBox。只需让它传播到应用程序,其中有更多的上下文来诊断错误。它们有责任捕获任何异常,以便应用程序不会崩溃。在异常的情况下,他们无法做任何事情,因为它与控件的内部有关,你应该自己处理,如果它部分是用户的错,请用一条消息说明缺少的内容,用原始文件包装通过InnerException属性可用的异常。

#4


0  

In addition to what's been said, I also want to mention that you should try to have your control avoid exceptions by checking for different object states and "preventing" rather then allowing an exception to be raised.

除了已经说过的内容之外,我还想提一下,你应该尝试让你的控件通过检查不同的对象状态和“防止”而不是允许引发异常来避免异常。

Keep mind mind that throwing an exception is a rather expensive process and (as I've been told in the past) exceptions should be reserved for truly "exceptional" unexpected cases...

请记住,抛出异常是一个相当昂贵的过程,并且(正如我过去所说的那样)异常应保留给真正“特殊”的意外情况......

Best Regards,
Frank

最好的问候,弗兰克

#1


3  

this is a common problem facing developers who build libraries. Try to weed out bugs and decide for the remaining error cases if it's an expected error (your control should not throw an exception but rather gracefully handle the error) or an unexpected exceptional condition (your control must throw an exception as soon as possible).

这是构建库的开发人员面临的常见问题。尝试清除错误并确定剩余的错误情况,如果它是预期的错误(您的控件不应抛出异常,而是优雅地处理错误)或意外的异常情况(您的控件必须尽快抛出异常)。

You might also have a look at Design By Contract, a methodology to declare required preconditions and guaranteed postconditions. This may sound academic, but it leads to more robust code.

您还可以查看Design By Contract,这是一种声明所需前置条件和保证后置条件的方法。这可能听起来很学术,但它会带来更强大的代码。

UPDATE: A good introduction is http://se.ethz.ch/~meyer/publications/computer/contract.pdf

更新:一个很好的介绍是http://se.ethz.ch/~meyer/publications/computer/contract.pdf

Regards, tamberg

#2


0  

unhandled exceptions should definitely be thrown so that the people using your control can see what's wrong.

绝对应该抛出未处理的异常,以便使用您的控件的人可以看到错误。

#3


0  

Only handle exceptions that you know of and know what to do with. Don't bother with generic handlers, like a MessageBox. Just let it propogate to the application where there's more context for diagnosing the error. It's their responsibility to catch any exceptions so that the application doesn't crash. In the case of an exception they can't do anything about because it has to do with the control's internals, you should handle that yourself and if it's partially the user's fault, wrap the exception with a message saying what is missing, with the original exception available via the InnerException property.

只处理您知道的并知道如何处理的异常。不要使用通用处理程序,如MessageBox。只需让它传播到应用程序,其中有更多的上下文来诊断错误。它们有责任捕获任何异常,以便应用程序不会崩溃。在异常的情况下,他们无法做任何事情,因为它与控件的内部有关,你应该自己处理,如果它部分是用户的错,请用一条消息说明缺少的内容,用原始文件包装通过InnerException属性可用的异常。

#4


0  

In addition to what's been said, I also want to mention that you should try to have your control avoid exceptions by checking for different object states and "preventing" rather then allowing an exception to be raised.

除了已经说过的内容之外,我还想提一下,你应该尝试让你的控件通过检查不同的对象状态和“防止”而不是允许引发异常来避免异常。

Keep mind mind that throwing an exception is a rather expensive process and (as I've been told in the past) exceptions should be reserved for truly "exceptional" unexpected cases...

请记住,抛出异常是一个相当昂贵的过程,并且(正如我过去所说的那样)异常应保留给真正“特殊”的意外情况......

Best Regards,
Frank

最好的问候,弗兰克