try..catch或尝试... catch..finally .. [英] try..catch or try...catch..finally..

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

问题描述

尝试..尝试或尝试...捕捉...最终......?



哪个更好,如果我想在我的代码中使用..

try..catch or try...catch..finally...?

which better if i want to use in my code..

推荐答案

请参阅我对该问题的评论。你应该只在你需要的地方使用一个表格,只在你需要的地方使用另一个表格,还可以试试表格。



只需阅读并彻底理解:< a href =http://msdn.microsoft.com/en-us/library/s7fekhdy.aspx> http://msdn.microsoft.com/en-us/library/s7fekhdy.aspx [< a href =http://msdn.microsoft.com/en-us/library/s7fekhdy.aspxtarget =_ blanktitle =New Window> ^ ]。



Catch经常被滥用。您应该仅在每个线程的帧堆栈之上捕获异常,并且在一些罕见的特殊情况下。您还应该在UI的主事件循环中捕获它。



请查看我过去的答案:

如何制作一个循环,滚动条到达底部 [ ^ ],

当我运行一个应用程序时,会发现异常如何处理这个问题? [ ^ ],

扔。 .then ... rethrowing [ ^ ],

处理类库(dll)中的异常 [ ^ ],

异常详细信息:System.Runtime.InteropServices.COMException:由于以下错误,检索具有CLSID {0006F03A-0000-0000-C000-000000000046}的组件的COM类工厂失败:... [ ^ ]。



新年快乐!



SA
Please see my comment to the question. You should use one form only where you need it, and another form only where you need it, and also try-catch form.

Just read and thoroughly understand: http://msdn.microsoft.com/en-us/library/s7fekhdy.aspx[^].

Catch is abused way to often. You should catch exceptions only on top of the frame stack of each thread, and in some rare special cases. You also should catch it in a main event loop of UI.

Please see my past answers:
How do i make a loop that will stop when a scrollbar reaches the bottom[^],
When i run an application an exception is caught how to handle this?[^],
throw . .then ... rethrowing[^],
Handling exceptions in class library (dll)[^],
Exception Details: System.Runtime.InteropServices.COMException: Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error:...[^].

Happy New Year!

—SA


这不是更好或更糟的情况,这取决于你的代码做什么

如果您的代码正在将数据写入文件,例如:

It isn''t a case of "better" or "worse" it depends on what your code does.
If your code is writing data to a file for example:
try
   {
   Files.WriteAllBytes(myData);
   }
catch (IOException ex)
   {
   MessageBox.Show(string.Format("Saving your data failed!\nThe system reported \"{0}\"", ex.Message));
   }

然后你不需要一个Finally块。

如果另一方面你打开了与数据库的连接:

Then you do not need a Finally block.
If on the other hand you open a connection to a database:

SqlConnection con = new SqlConnection(strConnection);
SqlCommand cmd = null;
try
   {
   con.Open();
   cmd = new SqlCommand(...
   }
catch (Exception ex)
   {
   MessageBox.Show(string.Format("Saving your data failed!\nThe system reported \"{0}\"", ex.Message));
   }
finally
   {
   if (con != null)
      {
      con.Close();
      }
   if (cmd != null)
      {
      cmd.Dispose();
      }
   }

然后一个 finally 块允许你确保它被关闭而不管发生了什么。

Then a finally block allows you to make sure it is closed regardless of what happens.


如果你有一些代码,必须执行(错误或没有错误)继续使用最后

那是'一般来说我的方法。
If you''ve got some code that must execute (error or no - error) go ahead and use Finally.
That''s generally my approach.


这篇关于try..catch或尝试... catch..finally ..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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