CA1031: 一般的な例外の種類はキャッチしません

TypeName

DoNotCatchGeneralExceptionTypes

CheckId

CA1031

分類

Microsoft.Design

互換性に影響する変更点

なし

原因

ExceptionSystemException などの汎用的な例外が catch ステートメントでキャッチされます。または catch() などの汎用的な catch 句が使用されています。

規則の説明

汎用的な例外はキャッチしないでください。

違反の修正方法

この規則違反を修正するには、より具体的な例外をキャッチするか、汎用的な例外は、catch ブロックの最後のステートメントでスローし直します。

警告を抑制する状況

この規則による警告は抑制しないでください。汎用的な例外の種類をキャッチすると、実行時の問題がライブラリを使用するときにわからなくなることがあります。また、デバッグが困難になることもあります。

[!メモ]

.NET Framework 4以降では、共通言語ランタイムは、Windows(CLR) のアクセス違反などのオペレーティング システムとマネージ コードは、マネージ コードによって処理されるときに発生する、破損した状態の例外を提供しません。.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: スタック詳細を保持するために再度スローします