冒泡异常 [英] Bubbling up exceptions

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

问题描述

大家好,


向某些界面处理程序冒泡时,


- 最值得推荐的是:


尝试

''一些代码

Catch ex As Exception

Throw

End试试




尝试

''一些代码

Catch ex As Exception

抛出ex

结束尝试




- 我只是想添加一些评论错误信息,

但是

我想保留所有内部异常(这样我就可以递归地将

吐到他们的
当我到达用户界面时,
用户)我如何正确地做到这一点?


- 并且,无论如何,投掷之后的任何事情都是正确的

被忽略了吗?


-P

解决方案



将某些异常冒泡到某些界面处理程序时


- 什么是最值得推荐的:


尝试

''一些代码

Catch ex As Exception

投掷

结束尝试




尝试

''一些代码

Catch ex As Exception

抛出ex

结束尝试



我'我不是VB程序员,但我不认为第一个例子应该编译。如果确实如此,那么我认为每个样本的行为是明白的,在这种情况下选择你最喜欢的那个:)


- 我只是想在错误消息中添加一些注释,

但是

我想保留所有内部异常(这样我就可以递归
当我到达用户界面时,
将它们吐到

用户)我该如何正确地做到这一点?



如果要在捕获异常时向UI显示信息,那么就这样做。不要重新抛出异常。只有

如果代码无法处理异常,则重新抛出异常。在这种情况下,您可以使用另一个异常包装异常

提供有关错误的更多信息。第二个参数为新的Exception提供了InnerException:

抛出新的异常(方法X中发生错误,试图执行操作Y.当时的变量A是: & A,ex)


此外,不要只是到处捕捉异常。框架中存在某些异常,您无法在代码中处理

而且不应该被捕获。因此,您应该只捕获可以由代码处理的那些类型的异常。


例如,如果您正在对您的System.IO.File对象执行某些操作尝试...捕获块然后你可以捕获

System.IO.IOException当且仅当您的代码可以正常处理该异常时。优雅意味着你的
程序的状态不会被破坏,或者在返回给调用者之前可以修复它。您还可以使用诸如无法将数据保存到文件:之类的简单消息更新UI,例如,

。 &安培; ex.Message。没有必要重新抛出IOException,因为你的代码已经优雅地处理了



- 并且,是否真的如此投掷之后无论如何会被忽略?
被忽略?



是的,同一方法体内的Throw语句之后的任何内容都不会被执行。


-

Dave Sexton




Dave Sexton ha scritto:




将某些异常冒泡到某些界面处理程序时


- 最值得推荐的:


尝试

''一些代码

Catch ex As Exception

Throw

结束尝试




尝试

''一些代码

Catch ex As Exception

抛出ex

结束尝试



我不是VB程序员,但我不认为第一个例子甚至应该编译。如果确实如此,那么我认为每个样本的行为

都是相同的,在这种情况下,选择你想要的更好:)



谢谢戴夫。 [实际上我错误地发布在这里。抱歉。对于VB组来说,消息是

。]


无论如何,在这一点上我对此中的一些陈述感到困惑

文章:
http://www.codeproject.com/dotnet /ex..tpractices.asp


作者说

扔掉前来

是错误的我应该总是使用

扔掉


所以我想听听那个意见....谢谢:)


-P


-

Dave Sexton




其中作者说

抛出前来

错了我应始终使用

抛出



如果不将变量分配给变量,则使用至少在C#中的ex而不使用:


试试

{

/ /做点什么

}

catch //没有变量赋值

{

throw; //在当前范围内抛出异常

}


我不确定VB中是否有等价物。


在C#中,下面的代码实际上是通过假设throw引用当前作用域中捕获的异常来编译的,就像上面代码中的

一样。无论是否将其赋值给变量,这都有效。


Catch ex As Exception

投掷

结束尝试



该代码将完全像投掷一样运行离"在你的另一个例子中。我不认为你选择哪一个很重要。


-

Dave Sexton


< ; pa *********** @ libero.itwrote in message news:11 ********************** @ i3g2000cwc.googlegro ups。 com ...


>

Dave Sexton ha scritto:


>


将一些异常冒泡到某些界面处理程序时


- 最值得推荐的:


尝试

''一些代码

Catch ex As Exception

Throw

结束尝试




尝试

''一些代码

Catch ex As Exception

抛出ex

结束尝试


我不是VB程序员,但我不认为是第一个例子甚至应该编译。如果确实如此,那么我认为每个样本的
行为都是相同的,在这种情况下选择你最喜欢的那个:)



谢谢戴夫。 [实际上我错误地发布在这里。抱歉。对于VB组来说,消息是

。]


无论如何,在这一点上我对此中的一些陈述感到困惑

文章:
http://www.codeproject.com/dotnet /ex..tpractices.asp


作者说

扔掉前来

是错误的我应该总是使用

扔掉


所以我想听听那个意见....谢谢:)


-P


> -
Dave Sexton



Hi guys,

When bubbling some exception up to some interface handlers

- what is most recommandable:

Try
''some code
Catch ex As Exception
Throw
End Try
or

Try
''some code
Catch ex As Exception
Throw ex
End Try
?

- What about I just to want to add some comments in the error message,
BUT
I want to keep ALL the inner exceptions (so that I can recursively
spit them to the
user when I arrive up to the UI ) How do I do that correctly ?

- And, is it true that anything after a Throw will be in any case
ignored ?

-P

解决方案

Hi,

When bubbling some exception up to some interface handlers

- what is most recommandable:

Try
''some code
Catch ex As Exception
Throw
End Try
or

Try
''some code
Catch ex As Exception
Throw ex
End Try

I''m not a VB programmer but I don''t think that the first example should even compile. If it does, then I assume that the behavior
of each sample is identicial and in that case choose whichever you like better :)

- What about I just to want to add some comments in the error message,
BUT
I want to keep ALL the inner exceptions (so that I can recursively
spit them to the
user when I arrive up to the UI ) How do I do that correctly ?

If you want to display information to the UI when you catch an exception then just do it. Don''t rethrow the Exception. Only
rethrow the exception if it cannot be handled by your code. In that case, you can wrap the exception with another exception that
provides some more information about the error. The second argument supplies the InnerException for the new Exception:

Throw new Exception("An error occurred in method X trying to perform operation Y. Variable A at the time was: " & A, ex)

Also, don''t just catch Exception everywhere. There are certain Exceptions in the framework that you won''t be able to handle in code
and should not be caught. For this reason you should only catch those Types of exceptions that can be handled by your code.

For instance, if you are performing some operation on the System.IO.File object in your Try...Catch block then you could catch
System.IO.IOException if and only if that exception can be handled gracefully by your code. Graceful means that the state of your
program will not be corrupt, or that it can be fixed before returning to the caller. You could also update the UI, for instance,
with a simple message like "Cannot save data to file: " & ex.Message. There is no need to rethrow the IOException since it has been
handled gracefully by your code.

- And, is it true that anything after a Throw will be in any case
ignored ?

Yes, anything after a Throw statement within the same method body will not be executed.

--
Dave Sexton



Dave Sexton ha scritto:

Hi,

When bubbling some exception up to some interface handlers

- what is most recommandable:

Try
''some code
Catch ex As Exception
Throw
End Try
or

Try
''some code
Catch ex As Exception
Throw ex
End Try


I''m not a VB programmer but I don''t think that the first example should even compile. If it does, then I assume that the behavior
of each sample is identicial and in that case choose whichever you like better :)

Thanks Dave. [Actually I posted here by error. Sorry. The message was
for the VB group.]

Anyway, on this point I have been confused by some statement in this
article:
http://www.codeproject.com/dotnet/ex...tpractices.asp

where the author says that
Throw ex
is wrong and I should always use
Throw

So I ''d like to hear opinions on that.... Thank you :)

-P

--
Dave Sexton


Hi,

where the author says that
Throw ex
is wrong and I should always use
Throw

Throw without ex, at least in C#, is used when you do not assign the exception to a variable:

try
{
// do something
}
catch // no variable assignment
{
throw; // throw exception in current scope
}

I''m not sure if there is an equivalent in VB.

In C# the following code actually does compile by assuming that throw refers to the exception caught in the current scope, just like
in the code above. This works regardless of whether it is assigned to a variable.

Catch ex As Exception
Throw
End Try

That code will function exactly as "Throw ex" in your other example. I don''t think it matters which one you choose.

--
Dave Sexton

<pa***********@libero.itwrote in message news:11**********************@i3g2000cwc.googlegro ups.com...

>
Dave Sexton ha scritto:

>Hi,

When bubbling some exception up to some interface handlers

- what is most recommandable:

Try
''some code
Catch ex As Exception
Throw
End Try
or

Try
''some code
Catch ex As Exception
Throw ex
End Try


I''m not a VB programmer but I don''t think that the first example should even compile. If it does, then I assume that the
behavior
of each sample is identicial and in that case choose whichever you like better :)


Thanks Dave. [Actually I posted here by error. Sorry. The message was
for the VB group.]

Anyway, on this point I have been confused by some statement in this
article:
http://www.codeproject.com/dotnet/ex...tpractices.asp

where the author says that
Throw ex
is wrong and I should always use
Throw

So I ''d like to hear opinions on that.... Thank you :)

-P

>--
Dave Sexton



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

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