مشاركة عبر


ca0059 استخدام ملف تكوين إلى تعريف مصدر بيانات

TypeName

DoNotCatchGeneralExceptionTypes

CheckId

ca1031

Category

Microsoft.تصميم

تعطيل تغيير

غير فاصلة

السبب

استثناء عام مثل System.Exceptionأو System.SystemExceptionهو تم مصادفة في catchالعبارة، أو شكل عام بجذب جملة بمثل catch()هو المستخدمة.

وصف القاعدة

يجب أن لا يتم حظر استثناءات عامة.

كيف إلى الإصلاح انتهاكات

إلى إصلاح انتهاكا لهذه قاعدة تصفية استثناء أكثر تحديداً أو re-رمى استثناء عام كالعبارة الأخيرة في catchحظر.

عند إلى منع التحذيرات

لا بمنع تحذير من هذه قاعدة. يمكن إخفاء catching أنواع استثناء عام مشاكل التشغيل من المكتبة مستخدم، و يعقد عملية التصحيح.

ملاحظة

ابتداء من .NET Framework الإصدار 4، CLR لم يعد يقدم الولاية تالف ‏‏ استثناءات التي تحدث في النظام التشغيل وتعليمات برمجية تمت إدارتها، مثل كـ الوصول انتهاكات في Windows، أن يتم معالجتها مع تعليمات برمجية مُدارة. إذا كنت ترغب في التحويل البرمجي ليطبق في .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: إعادة الطرح إلى الاحتفاظ بتفاصيل مكدس الذاكرة المؤقتة