你们如何处理类库中的错误? [英] How do you guys handle error's from class libraries?

查看:59
本文介绍了你们如何处理类库中的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我目前正在编写大量的类库,但不是主要的

应用程序。


我想提供一些方法来将错误报告回主要的

应用程序。


目前我有一个bool errBl我的课程中有一个字符串errMsg。

当errBl是真的时,可以看到errMsg中的错误。


它运行正常,但它相当笨重的。


你们是怎么做到的?触发事件或类似事件?


谢谢,

Andre

Hi all,

I''m currently writing a load of class libraries, but not the main
application iteslf.

I want to provide some method for reporting errors back to the main
application.

At the moment I have a bool errBl and a string errMsg in my classes.
When errBl is ture, one could see what the error was in errMsg.

It works ok, but its quite clunky.

How do you guys do it? Trigger an event or something like that?

Thank you,
Andre

推荐答案

每当发生组件(本例中的类

库)无法处理的错误时抛出异常。这个错误将冒泡。直到您的

客户端应用程序,您可以向用户显示错误并让他/她
决定重试该操作或取消。如果您愿意,您还可以在架构层中将此错误更高地追加到
。 .NET框架有许多从一般Exception类派生的

异常类。你可以通过继承Exception或它的一个派生来创建你自己的
。查找尝试,

" catch",throw和最后在MSDN中你会发现很多

的信息。我不明白你为什么要使用

事件,代表等创建自己的机制。有什么理由吗?
Throw an exception whenever an error occurs that your component (class
library in this case) can''t handle. This error will "bubble" up to your
client application where you can show the error to the user and let him/her
decide to retry the action or cancel. You can also trap this error higher up
in your architecture layer if you want to. The .NET framework has many
exception classes derived from the general Exception class. You can create
your own by subclassing Exception or one of its derivates. Look up "try",
"catch", "throw" and "finally" in the MSDN and you will find lots of
information. I can''t see why you should create your own mechanism using
events, delegates, etc. Any reasons for that?





一个选项是抛出异常,每次你检测到一些情况

,你无法恢复,抛出异常,它应该由

调用应用程序。

此外,您可以定义一个Log类,您可以在其中记录可能没有错误的事件,但可以帮助跟踪该程序。如果调用

应用程序初始化日志记录功能,则使用它,否则你什么都不做。

欢呼,


-

Ignacio Machin,

ignacio.machin AT dot.state.fl.us

佛罗里达州交通局


< ah **** @ gmail.com>在消息中写道

news:11 ********************** @ f14g2000cwb.googlegr oups.com ...
Hi,

One option is throwing exceptions, each time you detect some escenario
where you cannot recover, throw an exception, it should be handled by the
calling app.
In addition you could define a Log class, where you can log events that may
not be error but could help trace the working of the program. If the calling
app initialize the logging feature you use it, otherwise you do nothing.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

<ah****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
大家好,

我正在编写一大堆类库,但不是主要的应用程序。

我想提供将错误报告回主要应用程序的一些方法。

目前我的班级中有一个bool errBl和一个字符串errMsg。
当errBl真的有一个人可以看看errMsg中的错误是什么。

它工作正常,但它非常笨重。

你们是怎么做到的?触发事件或类似事件?

谢谢你,
Andre
Hi all,

I''m currently writing a load of class libraries, but not the main
application iteslf.

I want to provide some method for reporting errors back to the main
application.

At the moment I have a bool errBl and a string errMsg in my classes.
When errBl is ture, one could see what the error was in errMsg.

It works ok, but its quite clunky.

How do you guys do it? Trigger an event or something like that?

Thank you,
Andre



你好,


感谢您的回复。


是的,我现在使用try和catch来处理错误。

问题是我发现把这个错误带回

客户端非常笨拙。


我更多对冒泡感兴趣过程。


当我的装配很远时,如何将错误传达给客户

a层次结构。


假设主应用程序使用类A的实例,其中

使用类B的实例,它使用了一个实例

class C ....说D级发生错误,我怎么会把那个

错误带回到主要应用程序的C到B到A?


这样做看起来很笨拙。有没有办法将一个

消息从D直接传递回主应用程序,而不是通过C,B和A传递




也许是发送错误的中心位置,会在客户端触发事件




谢谢,

Andre


Frode写道:
Hi,

Thanks for the reply.

Yeah I use try and catch for handling the errors at the moment. The
issue is that I find it quite clumsy getting that error back to the
client.

I''m more interested in the "bubble up" process.

How do I get the error to the client when my assembly is quite far down
a hierarchy.

Say the main applications make use of an instance of class A, which
make use of an instance of class B, which make use of an instance of
class C.... Say an error occurred in class D, how would I get that
error back to C to B to A to the main application?

It seems quite clumsy doing it that way. Is there a way to pass a
message directly back to the main application from D, instead of going
through C, B, and A.

Maybe a central place for sending errors, which will trigger an event
on the client side?

Thanks,
Andre

Frode wrote:
当你的组件(在这种情况下是类库)发生错误时抛出一个异常不会处理。这个错误将冒泡。在您的客户端应用程序中,您可以向用户显示错误并让他/她决定重试该操作或取消。如果您愿意,还可以在架构层中将此错误更高地捕获。 .NET框架有许多从一般Exception类派生的异常类。您可以通过继承Exception或其衍生物之一来创建自己的。查找尝试,
捕获,抛出和最后在MSDN中你会发现很多
信息。我不明白为什么你应该使用
事件,代表等创建自己的机制。有什么理由吗?
Throw an exception whenever an error occurs that your component (class
library in this case) can''t handle. This error will "bubble" up to your
client application where you can show the error to the user and let him/her
decide to retry the action or cancel. You can also trap this error higher up
in your architecture layer if you want to. The .NET framework has many
exception classes derived from the general Exception class. You can create
your own by subclassing Exception or one of its derivates. Look up "try",
"catch", "throw" and "finally" in the MSDN and you will find lots of
information. I can''t see why you should create your own mechanism using
events, delegates, etc. Any reasons for that?






这篇关于你们如何处理类库中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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