CA1031: Allgemeine Ausnahmetypen nicht auffangen

TypeName

DoNotCatchGeneralExceptionTypes

CheckId

CA1031

Kategorie (Category)

Microsoft.Design

Unterbrechende Änderung

Nicht unterbrechend

Ursache

Allgemeine Ausnahmen, z. B. Exception oder SystemException, werden in einer catch-Anweisung oder mit einer allgemeinen catch-Klausel abgefangen, z. B. catch().

Regelbeschreibung

Allgemeine Ausnahmen sollten nicht abgefangen werden.

Behandeln von Verstößen

Um einen Verstoß gegen diese Regel zu beheben, fangen Sie eine spezifischere Ausnahme ab oder lösen die allgemeine Ausnahme in der letzten Anweisung des catch-Blocks erneut aus.

Wann sollten Warnungen unterdrückt werden?

Unterdrücken Sie keine Warnung dieser Regel.Durch das Abfangen allgemeiner Ausnahmetypen können Laufzeitprobleme vor dem Bibliotheksbenutzer verborgen werden und das Debuggen kann schwieriger werden.

HinweisHinweis

Ab .NET Framework 4, stellt die Common Language Runtime (CLR) nicht mehr beschädigte Zustandsausnahmen zu, die im Betriebssystem und in verwaltetem Code, beispielsweise Zugriffsverletzungen in Windows auftreten, von verwaltetem Code behandelt werden.Wenn Sie eine Anwendung in .NET Framework 4 oder höheren Versionen kompilieren und Behandlung von beschädigten Zustandsausnahmen beibehalten möchten, können Sie das HandleProcessCorruptedStateExceptionsAttribute-Attribut auf die Methode anwenden, die die beschädigte Zustandsausnahme behandelt.

Beispiel

Im folgenden Beispiel werden ein Typ, der gegen diese Regel verstößt, und ein Typ veranschaulicht, in dem der catch-Block korrekt implementiert wurde.

Imports System
Imports System.IO

Namespace DesignLibrary

    ' Creates two violations of the rule. 
    Public Class GenericExceptionsCaught

        Dim inStream  As FileStream 
        Dim outStream As FileStream 

        Sub New(inFile As String, outFile As String)

            Try
                inStream = File.Open(inFile, FileMode.Open)
            Catch ex As SystemException
                Console.WriteLine("Unable to open {0}.", inFile)
            End Try 

            Try
                outStream = File.Open(outFile, FileMode.Open)
            Catch
                Console.WriteLine("Unable to open {0}.", outFile)
            End Try 

        End Sub 

    End Class 

    Public Class GenericExceptionsCaughtFixed

        Dim inStream  As FileStream 
        Dim outStream As FileStream 

        Sub New(inFile As String, outFile As String)

            Try
                inStream = File.Open(inFile, FileMode.Open)

            ' Fix the first violation by catching a specific exception. 
            Catch ex As FileNotFoundException
                Console.WriteLine("Unable to open {0}.", inFile)
            End Try 

            Try
                outStream = File.Open(outFile, FileMode.Open)

            ' Fix the second violation by re-throwing the generic  
            ' exception at the end of the catch block. 
            Catch
                Console.WriteLine("Unable to open {0}.", outFile)
            Throw 
            End Try 

        End Sub 

    End Class 

End Namespace
using System;
using System.IO;

namespace DesignLibrary
{
    // Creates two violations of the rule. 
    public class GenericExceptionsCaught
    {
        FileStream inStream;
        FileStream outStream;

        public GenericExceptionsCaught(string inFile, string outFile)
        {
            try
            {
                inStream = File.Open(inFile, FileMode.Open);
            }
            catch(SystemException e)
            {
                Console.WriteLine("Unable to open {0}.", inFile);
            }

            try
            {
                outStream = File.Open(outFile, FileMode.Open);
            }
            catch
            {
                Console.WriteLine("Unable to open {0}.", outFile);
            }
        }
    }

    public class GenericExceptionsCaughtFixed
    {
        FileStream inStream;
        FileStream outStream;

        public GenericExceptionsCaughtFixed(string inFile, string outFile)
        {
            try
            {
                inStream = File.Open(inFile, FileMode.Open);
            }

            // Fix the first violation by catching a specific exception. 
            catch(FileNotFoundException e)
            {
                Console.WriteLine("Unable to open {0}.", inFile);
            }

            try
            {
                outStream = File.Open(outFile, FileMode.Open);
            }

            // Fix the second violation by re-throwing the generic  
            // exception at the end of the catch block. 
            catch
            {
                Console.WriteLine("Unable to open {0}.", outFile);
                throw;
            }
        }
    }
}
using namespace System;
using namespace System::IO;

namespace DesignLibrary
{
    // Creates two violations of the rule. 
    public ref class GenericExceptionsCaught
    {
        FileStream^ inStream;
        FileStream^ outStream;

    public:
        GenericExceptionsCaught(String^ inFile, String^ outFile)
        {
            try
            {
                inStream = File::Open(inFile, FileMode::Open);
            }
            catch(SystemException^ e)
            {
                Console::WriteLine("Unable to open {0}.", inFile);
            }

            try
            {
                outStream = File::Open(outFile, FileMode::Open);
            }
            catch(Exception^ e)
            {
                Console::WriteLine("Unable to open {0}.", outFile);
            }
        }
    };

    public ref class GenericExceptionsCaughtFixed
    {
        FileStream^ inStream;
        FileStream^ outStream;

    public:
        GenericExceptionsCaughtFixed(String^ inFile, String^ outFile)
        {
            try
            {
                inStream = File::Open(inFile, FileMode::Open);
            }

            // Fix the first violation by catching a specific exception. 
            catch(FileNotFoundException^ e)
            {
                Console::WriteLine("Unable to open {0}.", inFile);
            }

            try
            {
                outStream = File::Open(outFile, FileMode::Open);
            }

            // Fix the second violation by re-throwing the generic  
            // exception at the end of the catch block. 
            catch(Exception^ e)
            {
                Console::WriteLine("Unable to open {0}.", outFile);
                throw;
            }
        }
    };
}

Verwandte Regeln

CA2200: Erneut ausführen, um Stapeldetails beizubehalten