編譯器警告 (層級 1) CS1058
更新:2007 年 11 月
錯誤訊息
之前的 catch 子句已經取得所有例外狀況。所有擲回的例外狀況都將包裝在 System.Runtime.CompilerServices.RuntimeWrappedException 中
如果 catch() 區塊未在 catch (System.Exception e) 區塊後指定例外狀況類型,則這個屬性 (Attribute) 會產生 CS1058 錯誤。這個警告指示 catch() 區塊將不會攔截任何例外狀況。
如果 AssemblyInfo.cs 檔案內的 RuntimeCompatibilityAttribute 設定為 false,則 catch (System.Exception e) 區塊後的 catch() 區塊可以攔截非 CLS 例外狀況:[assembly: RuntimeCompatibilityAttribute(WrapNonExceptionThrows = false)]。如果這個屬性未明確設定為 false,則所有擲回的非 CLS 例外狀況會包裝為例外狀況,並由 catch (System.Exception e) 區塊攔截這些例外狀況。如需詳細資訊,請參閱 HOW TO:攔截非 CLS 例外狀況。
範例
下列範例會產生 CS1058。
// CS1058.cs
// CS1058 expected
using System.Runtime.CompilerServices;
// the following attribute is set to true by default in the C# compiler
// set to false in your source code to resolve CS1058
[assembly: RuntimeCompatibilityAttribute(WrapNonExceptionThrows = true)]
class TestClass
{
static void Main()
{
try {}
catch (System.Exception e) {
System. Console.WriteLine("Caught exception {0}", e);
}
catch {} // CS1058. This line will never be reached.
}
}