Não capturar tipos de exceção geral
TypeName |
DoNotCatchGeneralExceptionTypes |
CheckId |
CA1031 |
Category (Categoria) |
Microsoft.Design |
Quebrando alterar |
Não separável |
Causa
System.Exception ou System.SystemException pego em um catch demonstrativo ou uma cláusula catch geral é usado.
Descrição da regra
Exceções Geral não devem ser detectadas.
Como corrigir violações
Para corrigir uma violação dessa regra, capturar uma exceção mais específica ou relançar a exceção geral sistema autônomo a última demonstrativo no catch bloco.
Quando suprimir avisos
Não suprimir um aviso da regra.Detectar tipos de exceção geral podem ocultar os problemas de time de execução a partir do usuário da biblioteca e podem complicar a depuração.
Exemplo
O exemplo a seguir mostra um tipo que violam essa regra e um tipo que implementa o corretamentecatch bloco.
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}.", inFile)
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;
}
}
};
}