处理异常 [英] Handling exceptions

查看:53
本文介绍了处理异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


我对如何处理异常有点困惑。我目前正在构建一个ADO.NET Windows Forms应用程序。
我已经构建了一个类来操作数据库中的数据。这个类用于表单中的
。我们假设存在一个SQL错误:这会抛出一个

异常。我应该在哪里处理它?我应该怎么处理?在给出错误信息时显示什么




是否有任何集中的信息?处理异常的方法:例如,

有一个集中的异常处理程序捕获所有内容,并显示相对于捕获的异常的

错误消息?

谢谢

Hello

I''m a bit confused on how one should handle exceptions. I''m currently
building an ADO.NET Windows Forms application. I''ve built a class for
manipulating data within the database in question. This class is used
from forms. Let''s assume that there is an SQL error: this will throw an
exception. Where should I handle it? How should I handle it? What to
show when giving an error message?

Is there any "centralized" way of handling exceptions: for example,
having a centralized exception handler catch everything, and show an
error message relative to the caught exception?

Thanks

推荐答案

好的这是一个多方答案的问题,因为没有正确的方法来处理

所有例外,一个对我有用的例外是:

首先,如果Exception是一个预期的异常句柄,它接近代码

,它给出了例外。

第二,如果你可以从异常中恢复,那就在它投掷时接近它。

如果你不知道或期望异常并因为你正在使用windows

表单你可以在应用程序对象中设置一个全局异常处理程序,比如

这个:

Application.SetUnhandledExceptionMode(UnhandledExc eptionMode.Automatic);

Application.ThreadException + = new

System.Threading.ThreadExceptionEventHandler(Appli cation_ThreadException);

static void Application_ThreadException(object sender,

System.Threading.ThreadExceptionEventArgs e)

{

//处理这里一个未处理的例外

}


那里你将处理一个意外的异常,但有一个问题,你

需要知道申请是否可以在例外之后继续。


问候,


Bela Istok

" Sweetiecakes" < x@x.com写在留言中

news:48 *********************** @ news.fv.fi。 ..
Ok this is a multi answer question because there is no a right way to handle
all the exceptions, one that works for me is:
First if the Exception is an expected exception handle that near the code
that gives the Exception.
Second if you can recover from the exception do it near when it''s throwing.
If you don''t know or expect the exception and because you are using windows
form you can set a Global exception handler in the application object like
this:
Application.SetUnhandledExceptionMode(UnhandledExc eptionMode.Automatic);
Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(Appli cation_ThreadException);
static void Application_ThreadException(object sender,
System.Threading.ThreadExceptionEventArgs e)
{
//Handle here an Unhandled Exception
}

And there you will handle an unexpected exception, but there is a catch, you
need to know if the application can continue after the exception or not.

Regards,

Bela Istok
"Sweetiecakes" <x@x.comwrote in message
news:48***********************@news.fv.fi...

你好


我对如何应对异常感到困惑。我目前正在构建一个ADO.NET Windows Forms应用程序。
我已经构建了一个类来操作数据库中的数据。这个类用于

表格。我们假设存在一个SQL错误:这会抛出一个

异常。我应该在哪里处理它?我应该怎么处理?什么显示

给出错误信息?


有没有集中的?处理异常的方法:例如,拥有

a集中式异常处理程序捕获所有内容,并显示错误

消息相对于捕获的异常?


谢谢
Hello

I''m a bit confused on how one should handle exceptions. I''m currently
building an ADO.NET Windows Forms application. I''ve built a class for
manipulating data within the database in question. This class is used from
forms. Let''s assume that there is an SQL error: this will throw an
exception. Where should I handle it? How should I handle it? What to show
when giving an error message?

Is there any "centralized" way of handling exceptions: for example, having
a centralized exception handler catch everything, and show an error
message relative to the caught exception?

Thanks


SweetieCakes,


你问的一个非常复古的问题,我想是关于70'的。


但是你当然可以制作一种方法来处理那种通用的方式,并传递异常。


我不知道它是不是只是在说话,但是一个SQL例外应该只是设计时间,因为它会抓住你所犯的错误你的

剧本。

Cor


" Sweetiecakes" < x@x.comschreef in bericht

news:48 *********************** @ news.fv.fi。 ..
SweetieCakes,

A very retro question you ask, I think from about the 70''s.

But you can of course make a method that handles those in a kind of general
way and pass the exception.

I don''t know if it was just speaking words, but an SQL exception should only
be in design time, because it catches the errors you have made in your
scripts.
Cor

"Sweetiecakes" <x@x.comschreef in bericht
news:48***********************@news.fv.fi...

你好


我对如何应对异常感到困惑。我目前正在构建一个ADO.NET Windows Forms应用程序。
我已经构建了一个类来操作数据库中的数据。这个类用于

表格。我们假设存在一个SQL错误:这会抛出一个

异常。我应该在哪里处理它?我应该怎么处理?什么显示

给出错误信息?


有没有集中的?处理异常的方法:例如,拥有

a集中式异常处理程序捕获所有内容,并显示错误

消息相对于捕获的异常?


谢谢
Hello

I''m a bit confused on how one should handle exceptions. I''m currently
building an ADO.NET Windows Forms application. I''ve built a class for
manipulating data within the database in question. This class is used from
forms. Let''s assume that there is an SQL error: this will throw an
exception. Where should I handle it? How should I handle it? What to show
when giving an error message?

Is there any "centralized" way of handling exceptions: for example, having
a centralized exception handler catch everything, and show an error
message relative to the caught exception?

Thanks


Cor Ligthert [MVP]写道:
Cor Ligthert[MVP] wrote:

我不喜欢不知道它是不是只是在说话,但是一个SQL异常应该只是在设计时间内,因为它会捕获你在脚本中所做的错误。。 br />
I don''t know if it was just speaking words, but an SQL exception should
only be in design time, because it catches the errors you have made in
your scripts.



我现在在你的服务器房间*,拔掉你的网络。让我们看看你的用户如何使用神秘的错误消息。 :-)


-

J.

I''m in your server room *right now*, unplugging your network. Let''s see how
your users like cryptic error messages. :-)

--
J.


这篇关于处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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