抛出例外 [英] Throwing Exceptions

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

问题描述

我有一个关于抛出异常的查询。


要抛出异常,我输入类似的内容:


try

{

//做某事

}

catch(ArgumentOutOfRangeException)

{

抛出新的异常(我的异常);

}


当尝试部分失效时,我的被抛出,如果不是法庭

elswair将在调试和点亮线时显示:


抛出新的例外(我的例外);

但是当使用微软框架库中的其他类

时,高亮显示的行是调用包含该异常的类

的行。


我想说的是,当我的异常被抛出时我没有那个

它已经抛出了豆子但是我不知道是什么诅咒了它扔,所以它是

恐惧使用在那里。


任何sugestions。


Tha nks,Jay Dee。

I have a query about throwing exceptions.

To throw an exception I type something like:

try
{
// do somthing
}
catch (ArgumentOutOfRangeException)
{
throw new Exception("My Exception");
}

And when the try section fales my excepton is thrown and if not court
elswair will display when debuging and hi-light the line:

throw new Exception("My Exception");

but when using other classes like in the microsoft framework library
the line that is hi-lighted is the line that called the class
containing that exception.

What I am trying to say is that when my exception is thrown I no that
it has bean thrown but I don''t know what corsed it to throw, so it is
fearly useles being there.

Any sugestions.

Thanks, Jay Dee.

推荐答案

几件事我不知道为什么你会抓到一个特定的例外和

将它重新抛出作为一般例外 - 原因是什么?对于你的问题,

你需要做的是查看exception.Message属性和它

应该包含字符串my exception这通常是你抛出异常时设置

的原因。有意义吗?


-

问候,

Alvin Bruney

----- ------------------------------------------------- < br $> b $ b无耻的作者插件

OWC黑皮书第2版

仅限 www.lulu.com/owc
couple things i am not sure why you are catching a specific exception and
rethrowing it as a general exception - reason for this? To your question,
what you will need to do is view the exception.Message property and it
should contain the string "my exception" which is normally the reason that
you set when you throw an exception. makes sense?

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless Author Plug
OWC Black Book 2nd Edition
Exclusively on www.lulu.com/owc


24.99

Jay Dee ; < fi ****** @ gmail.com写信息

news:11 ********************** @ g4g2000hsf .googlegro ups.com ...
24.99
"Jay Dee" <fi******@gmail.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...

>我有关于抛出异常的查询。


抛出异常我打字类似:


试试

{

//做某事

}

catch(ArgumentOutOfRangeException)

{

抛出新的异常(我的异常);

}


当尝试部分出现故障时,我的投掷将被抛出,如果不是法院

elswair将在调试时显示并点亮该线:

抛出新的异常(我的异常);


但是当使用像微软框架库中的其他类时

那行高亮的是打电话给班级

包含该例外的行。


我想说的是,当我的异常被抛出时我不那个

哈哈抛出的豆子,但我不知道是什么让它扔掉了,所以它是b / b
恐惧使用在那里。


任何sugestions。


谢谢,Jay Dee。
>I have a query about throwing exceptions.

To throw an exception I type something like:

try
{
// do somthing
}
catch (ArgumentOutOfRangeException)
{
throw new Exception("My Exception");
}

And when the try section fales my excepton is thrown and if not court
elswair will display when debuging and hi-light the line:

throw new Exception("My Exception");

but when using other classes like in the microsoft framework library
the line that is hi-lighted is the line that called the class
containing that exception.

What I am trying to say is that when my exception is thrown I no that
it has bean thrown but I don''t know what corsed it to throw, so it is
fearly useles being there.

Any sugestions.

Thanks, Jay Dee.



Jay Dee写道:
Jay Dee wrote:

[...]

当试用部分失效时,我的投掷将被抛出,如果不是法院的话

elswair将在debuging和hi时显示点亮这一行:


抛出新的异常(我的异常);


但是当使用像microsoft框架库中的其他类时

高亮的线是调用包含该异常的类

的线。
[...]
And when the try section fales my excepton is thrown and if not court
elswair will display when debuging and hi-light the line:

throw new Exception("My Exception");

but when using other classes like in the microsoft framework library
the line that is hi-lighted is the line that called the class
containing that exception.



差别主要是因为你没有.NET代码的源代码

。因此,调试器会向您显示最接近抛出异常的源代码

。如果您自己的代码抛出异常,那么该代码就是抛出异常的代码。但是如果其他一些

代码抛出一个异常,那么最接近的代码仍将在你自己的代码中,在那里它调用了源代码不存在的代码。 br />

The difference there is basically because you don''t have the source code
to the .NET code. So the debugger shows you the source code that is
closest to the thrown exception. If your own code throws an exception,
then that code is the code that threw the exception. But if some other
code throws an exception, then the closest code will still be in your
own code where it called the code for which the source code is absent.


我想说的是,当我的异常被抛出时我没有那个

它有豆扔了但我不知道什么强迫它抛出,所以它是b / b
恐惧使用在那里。
What I am trying to say is that when my exception is thrown I no that
it has bean thrown but I don''t know what corsed it to throw, so it is
fearly useles being there.



这是对的。正如阿尔文所说的那样,为什么你这样编写

代码并不清楚。你有一个非常好的例外。如果你不是要处理它(也就是从它中恢复)那么为什么要用

全新的异常隐藏它?特别是一个通用的,而不是原始的一个。


至少,你可以写throw;而不是抛出新的

异常(我的例外);至少这会导致异常

被抛出与被捕获的异常相同。如果我记得,

堆栈跟踪将是错误的,但如果你修复了catch,那么语句,以便

它声明一个本地ArgumentOutOfRangeException变量,然后至少

你可以在调试器中查看该变量并查看堆栈

跟踪是(但是,只有当你在自己的抛出

语句之前放置一个断点时)。


我的经验是它重新抛出

异常或从catch中抛出新异常几乎总是错误的。条款。有时

这是你想做的事情,但通常最好只是在catch中本地处理

错误。条款或放置任何清理代码

认为应该在catch中条款进入终于条款

而不是任何例外。


Pete

That''s right. As Alvin says, it''s not at all clear why you write the
code like that. You had a perfectly good exception. If you''re not
going to handle it (that is, recover from it) then why hide it with a
whole new exception? Especially one that is a generic one, rather than
the original one.

At the very least, you could just write "throw;" instead of "throw new
Exception("My Exception");" At least that would cause the exception
thrown to be the same as the one that was caught. If I recall, the
stack trace will be wrong, but if you fix the "catch" statement so that
it declares a local ArgumentOutOfRangeException variable, then at least
you can look at that variable in the debugger and see what the stack
trace is (but only if you put a breakpoint before your own "throw"
statement, of course).

My experience has been that it''s almost always a mistake to rethrow an
exception or throw a new exception from a "catch" clause. Sometimes
it''s what you want to do, but usually it''s better to either just handle
the error locally in the "catch" clause or put whatever cleanup code you
thought should go in the "catch" clause into the "finally" clause
instead, and just not catch any exception.

Pete


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

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