开发者

Ok to put Exception in EventArgs?

I'm building a messenger library, and I have a question about how properly to report specific errors to the user.

The main Messenger class has two states - logged in, and logged out.

Errors that occur internally can cause the client to logout.

For example, a DisconnectException (i开发者_运维知识库f a SocketException occurs internally), or a ChallengeFailedException if the client is unable to complete a challenge issued by the messenger server.

Is it ok (or recommended) to pass the exception to the user in the LogoutEventArgs ? for instance - LogoutEventArgs.Exception

Or is there a more ideal way to handle this?


If the exception isn't expected (i.e., it really is an exceptional circumstance) then you should probably just let the exception be thrown so that the user can use a catch statement to deal with any specific exception types.

If the exception is thrown from another thread, you can wrap it in another exception and throw it on the calling thread.

If it is an expected, common error condition then it depends on what the user is supposed to do in that case. Something like an enumeration or a custom Error class might be better than just sticking the exception in the EventArgs object. The custom object can include a reference to the Exception object as well.


I have a class that swallows some known exceptions such as sql server time out. This is so my clients don't need to deal with this, my class catches it and returns an error enum. I event out the caught exceptions solely so my clients can opt to log them. I don't want my class to have logging dependencies.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜