不能离开一个finally子句的正文 [英] Cannot leave the body of a finally clause

查看:67
本文介绍了不能离开一个finally子句的正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,



如果我在try catch finally块的finally子句中放入一个return语句,我会收到如下错误:



Hello friends,

If i put a return statement in a finally clause of a try catch finally block, i get an error like below:

引用:

不能离开finally子句的正文

Cannot leave the body of a finally clause







假设:






Suppose:

try
  {


   }

 catch(SomeException e)
  {



   }

 finally
  {


  return some_value;

  }





有人可以解释其背后的原因吗?我删除了错误。我不知道为什么会这样?背后的原因是什么?



谢谢



Can some one please explain the reason behind it? I removed the error. Bu i am inquisitive as to why does this happen? whats the reason behind it?

Thanks

推荐答案

问候,



编译器不允许你从finally子句中退出函数。你可以在最后的cluase中找到清理代码。





Greetings,

The compiler does not allow you exit the function from a finally clause. You can have cleanup code in finally cluase.


try
  {
 

   }
 
 catch(SomeException e)
  {
 
 
   }
 
 finally
  {
 //clean up code
 
  }

  return some_value;





问候



Dominic



Regards

Dominic


异常将退出函数,而不是返回。您可以通过返回或异常退出函数,但不能同时退出两者。这就是为什么你不能从最后回来的原因。


你可以做下面的事情: -



The exception is going to exit the function, not the return. You can exit a function via a return or an exception, but not both. That''s why you can''t return from a finally.

you can do something like below:-

 try
  {

   }

 catch(SomeException e)
  {

   }

 finally
  {
  
  }
return some_value;


无法在C#中使用'return in a finally块'是语言设计选择;其他语言,如Java,JavaScript,允许它。事实上,这两种语言都允许在catch和finally块中返回:定义的最后一个返回是执行的内容。



一些例外与OutOfMemoryException类似,它将在执行finally块代码之前终止执行,但是,通常,finally块中的代码保证执行。你应该使用finally块来清理以处理需要处理的东西。



你可以使用'返回catch区块,但是,如果你有一个finally块和一个catch块,它设置了一个变量(在方法之外声明)在一个方法返回之后在catch块中被修改的有趣可能性。你可以在这里看到一个例子:[ ^ ]。 />


如果你真的想深入了解.NET编译器如何转换为try-catch-finally到IL代码,那么上面提到的线程链接是非常有价值的。



请注意,有些人对.NET中应该完成错误处理的方式有很强烈的感受,并且他们主张总是抛出一些例外,直到一些更高级别的异常处理程序如果你想看的话,CodeProject上有关于高级应用程序范围错误处理策略的资源。



实际上,我会说只使用返回 finally块之后,如果你在catch块中处理了错误,那么这是一个最好的做法。
Not being able to use 'return in a 'finally block in C# is a language-design choice; other languages, like Java, JavaScript, allow it. In fact, both those languages allow returns in both catch and finally blocks: the last return defined is what's executed.

There are some exceptions, like OutOfMemoryException, that will terminate execution before a finally block code is executed, but, in general, the code in a finally block is guaranteed to execute. You should use a finally block to "clean up," to dispose of things that need disposing.

You can use 'return in the catch block, but, if you have a finally block and a catch block, that sets up the interesting possibility of a variable (declared outside the method) being modified in the catch block after a method has returned. You can see an example of that here: [^].

If you really want to go deep into how the .NET compiler under-the-hood transforms to try-catch-finally to IL code, the thread of the above-cited link is quite valuable.

Note that there are some people who have very strong feelings about the way error handling should be done in .NET, and that they advocate always throwing an exception up to some higher level exception handler. There are resources on advanced application-wide error handling strategies here on CodeProject if you care to look.

On a practical basis, I'd say that using return only after the finally block, if you have handled the error in a catch block, is a best practice.


这篇关于不能离开一个finally子句的正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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