.NET/C# 異常處理:寫一個空的 try 塊代碼,而把重要代碼寫到 finally 中

不知你是否見過 try { } finally { } 代碼中,try 塊留空,而只往 finally 中寫代碼的狀況呢?這種寫法有其特殊的目的。html

本文就來講說這種不同的寫法。java


你能夠點開這個連接查看 Exception 類,在裏面你能夠看到一段異常處理的代碼很是奇怪:git

// 代碼已通過簡化。 internal void RestoreExceptionDispatchInfo(ExceptionDispatchInfo exceptionDispatchInfo) { // 省略代碼。 try{} finally { // 省略代碼。 } // 省略代碼。 } 

神奇之處就在於,其 try 塊是空的,重要代碼都放在 finally 中。那爲何會這麼寫呢?github

在代碼註釋中的解釋爲:web

We do this inside a finally clause to ensure ThreadAbort cannot be injected while we have taken the lock. This is to prevent unrelated exception restorations from getting blocked due to TAE.c#

翻譯過來是:ide

在 finally 子句中執行此操做以確保在獲取鎖時沒法注入 ThreadAbort。這是爲了防止不相關的異常恢復因 TAE 而被阻止。post

也就是說,此方法是爲了與 Thread.Abort 對抗,防止 Thread.Abort 中斷此處代碼的執行。Thread.Abort 的執行交給 CLR 管理,finally 的執行也是交給 CLR 管理。CLR 確保 finally塊執行的時候不會被 Thread.Abort 阻止。this

代碼在 .NET Core 和 .NET Framework 中的實現徹底同樣:atom

// This is invoked by ExceptionDispatchInfo.Throw to restore the exception stack trace, corresponding to the original throw of the // exception, just before the exception is "rethrown". [SecuritySafeCritical] internal void RestoreExceptionDispatchInfo(System.Runtime.ExceptionServices.ExceptionDispatchInfo exceptionDispatchInfo) { bool fCanProcessException = !(IsImmutableAgileException(this)); // Restore only for non-preallocated exceptions if (fCanProcessException) { // Take a lock to ensure only one thread can restore the details // at a time against this exception object that could have // multiple ExceptionDispatchInfo instances associated with it. // // We do this inside a finally clause to ensure ThreadAbort cannot // be injected while we have taken the lock. This is to prevent // unrelated exception restorations from getting blocked due to TAE. try{} finally { // When restoring back the fields, we again create a copy and set reference to them // in the exception object. This will ensure that when this exception is thrown and these // fields are modified, then EDI's references remain intact. // // Since deep copying can throw on OOM, try to get the copies // outside the lock. object _stackTraceCopy = (exceptionDispatchInfo.BinaryStackTraceArray == null)?null:DeepCopyStackTrace(exceptionDispatchInfo.BinaryStackTraceArray); object _dynamicMethodsCopy = (exceptionDispatchInfo.DynamicMethodArray == null)?null:DeepCopyDynamicMethods(exceptionDispatchInfo.DynamicMethodArray); // Finally, restore the information. // // Since EDI can be created at various points during exception dispatch (e.g. at various frames on the stack) for the same exception instance, // they can have different data to be restored. Thus, to ensure atomicity of restoration from each EDI, perform the restore under a lock. lock(Exception.s_EDILock) { _watsonBuckets = exceptionDispatchInfo.WatsonBuckets; _ipForWatsonBuckets = exceptionDispatchInfo.IPForWatsonBuckets; _remoteStackTraceString = exceptionDispatchInfo.RemoteStackTrace; SaveStackTracesFromDeepCopy(this, _stackTraceCopy, _dynamicMethodsCopy); } _stackTraceString = null; // Marks the TES state to indicate we have restored foreign exception // dispatch information. Exception.PrepareForForeignExceptionRaise(); } } } 

你能夠在 這裏 查看 .NET Framework 版本,在這裏 查看 .NET Core 的版本。


參考資料

原文地址: https://walterlv.com/post/empty-try-block.html

做者:呂毅

相關文章
相關標籤/搜索