Kommentar
Åtkomst till den här sidan kräver auktorisering. Du kan prova att logga in eller ändra kataloger.
Åtkomst till den här sidan kräver auktorisering. Du kan prova att ändra kataloger.
En tidigare catch-sats fångar redan alla undantag. Alla undantag som genereras kommer att omslutas i en System.Runtime.CompilerServices.RuntimeWrappedException
Det här attributet orsakar CS1058 om ett catch() block inte har någon angiven undantagstyp efter ett catch (System.Exception e) block. Varningen rekommenderar att catch() blocket inte fångar upp några undantag.
Ett catch() block efter ett catch (System.Exception e) block kan fånga icke-CLS-undantag om RuntimeCompatibilityAttribute är inställt på false i filen AssemblyInfo.cs: [assembly: RuntimeCompatibilityAttribute(WrapNonExceptionThrows = false)]. Om det här attributet inte uttryckligen anges till false omsluts alla icke-CLS-undantag som undantag och catch (System.Exception e) blocket fångar upp dem. Mer information finns i Så här fångar du ett undantag som inte är CLS.
Exempel
I följande exempel genereras 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.
}
}