共用方式為


CA1031:不要攔截一般例外狀況型別

型別名稱

DoNotCatchGeneralExceptionTypes

CheckId

CA1031

分類

Microsoft.Design

中斷變更

中斷

原因

catch 陳述式中攔截了一般例外狀況,如 System.ExceptionSystem.SystemException;或使用了一般的 catch 子句,如 catch()。

規則描述

不應該攔截一般例外狀況。

如何修正違規

若要修正此規則的違規情形,請攔截較明確的例外狀況,或以 catch 區塊中的最後一個陳述式,重新擲回一般例外狀況。

隱藏警告的時機

請勿隱藏此規則的警告。 攔截一般例外狀況型別會向程式庫使用者隱藏執行階段的問題,因而使偵錯變得更困難。

注意事項注意事項

自 .NET Framework 4 版 開始,Common Language Runtime (CLR) 不再傳遞發生在作業系統和 Managed 程式碼中的毀損狀態例外狀況 (例如 Windows 中的存取違規) 給 Managed 程式碼來處理。如果您想要在 .NET Framework 4 (含) 以後版本中編譯應用程式,並保留毀損狀態例外狀況處理,您可以將 HandleProcessCorruptedStateExceptionsAttribute 屬性套用至處理毀損狀態例外狀況的方法。

範例

在下列範例中,程式碼會顯示違反此規則的型別,以及正確實作 catch 區塊的型別。

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;
            }
        }
    };
}

相關規則

CA2200:請重新擲回以保存堆疊詳細資料