尝试Catch并最终阻止Excecution [英] Try Catch and finally block Excecution

查看:113
本文介绍了尝试Catch并最终阻止Excecution的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专家,



我在此之前通过谷歌发布了一个问题,但没有得到任何满意的答案。

。我有以下三个条件。

Hi Experts,

I go through Google prior to post a question here but not got any satisfactory answer.
. I have below three conditions.

private string  ExcetionTest()
		{
			
			try
			{
				throw new Exception("E1");
			}
			catch (Exception ex)
			{

				throw new Exception("E2");
			}
			finally
			{
				throw new Exception("E3");
			}
		}





Case1。



Case1.

private void button4_Click(object sender, EventArgs e)
{
		This. ExcetionTest();
}





Case2。



Case2.

private void button4_Click(object sender, EventArgs e)
{
		try
			{
				This. ExcetionTest();

			}
			catch (Exception ex)
			{
				Throw ex;
			}
			finally
			{
				MessageBox.Show("finally");
			}

}





Case3。



Case3.

private void Form1_Load(object sender, EventArgs e)
{
			try
			{
				throw new Exception("E1");
			}
			catch (Exception ex)
			{

				throw new Exception("E2");
			}
			finally
			{
				throw new Exception("E3");
			}
	
	
}





当我开始调试第一个案例时,finally块永远不会被击中,控制在拦截块中停止。

在调试第二种情况时,尝试catch并最终调试ExcetionTest()方法正确调试,但外部最后再次永远不会被命中。

案例3真的不可理解当我开始在表单加载事件中调试相同的代码时,尝试catch并最终正确执行块。这里的问题是案例1和案例3之间的区别是什么?为什么最后点击case.3但不包括1.



谢谢,



When I start debugging first case the finally block never be hit, control stopped in catch block.
When debugging the second case, try catch and finally block of ExcetionTest() method be debug properly but outer finally again never be hit.
The case 3 is really not understandable when I start debugging the same code in form load event, try catch and finally block executed properly. Here question is what is the difference between case 1 and case 3? Why finally hit in case.3 but not incase 1.

Thanks,

推荐答案

它有点你在这里遇到的一般情况。



案例1控件不只是停止 - 你得到一个未处理的异常错误 - 这正是它所说的 - Visual Studio不知道你要做什么,除了你正在提高。



案例2 VS知道调用进程有一个try-catch所以它可以向上冒泡。它可以处理当前异常的finally子句并将新异常传递给该行。然后你在调用按钮的catch子句中遇到未处理的异常问题。



关键是你开始在catch和/或最后,当前异常的处理停止了(这有一些例外情况,但它们超出了这个问题的范围)。



表单加载情况确实让我有点困惑 - 我本来期望它的行为与Case 2相同。但是如果仔细观察VS中的输出框架,你会看到两个异常都被引发了。但正如我所说,这是一个相当人为的情况,我绝不会期望在真实代码中看到。



您可能会发现这些主题的文章很有用。 CodeProject:

C#初学者的异常处理 [ ^ ]

.NET中的异常处理最佳实践 [< a href =http://www.codeproject.com/Articles/9538/Exception-Handling-Best-Practices-in-NETtarget =_ blanktitle =New Window> ^ ]

MSDN:

例外情况和异常处理 [ ^ ]

最佳例外做法 [ ^ ]
It's a bit of an usual situation you have here.

In Case 1 control does not just "stop" - you get an "unhandled exception" error - which is exactly what it says - Visual Studio has no idea what you want to do with the exception that you are raising.

In Case 2 VS is aware that the calling process has a try-catch so it can bubble the exception upwards. It can process the finally-clause for current exception and pass the new exception up the line. You then hit the "unhandled exception" problem in the calling button's catch-clause.

The key thing is that once you start throwing exceptions within the catch and/or the finally, processing of the current exception ceases (there are certain exceptions to this, but they are out of scope for this problem).

The form load situation does have me a little confused - I would have expected it to behave in the same way as Case 2. But if you look closely in the output frame in VS you will see that both exceptions have been raised. But as I said, this is a rather contrived situation that I would never expect to see in "real" code.

You may find these articles on the subject useful. CodeProject:
Exception Handling for C# Beginners[^]
Exception Handling Best Practices in .NET[^]
MSDN:
Exceptions and Exception Handling[^]
Best Practices for Exceptions[^]


只需几个注释:所有最后分支没有意义,因为它们永远不会到达。关键是你只使用一种异常类型, System.Exception ,它涵盖了所有可能异常的100%。这个事实使你的研究没有真正有效,几乎无用。你必须前进。



-SA
Just few notes: all finally branches makes no sense, because they are never reach. The key is that you are using only one exception type, System.Exception which covers 100% of all possible exception. This fact makes your research no really productive, almost useless. You have to go forward.

—SA


这篇关于尝试Catch并最终阻止Excecution的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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