CA1031:不要捕捉一般异常类型
类型名 |
DoNotCatchGeneralExceptionTypes |
CheckId |
CA1031 |
类别 |
Microsoft.Design |
是否重大更改 |
非重大更改 |
原因
在 catch 语句中捕捉到一般异常,如 System.Exception 或 System.SystemException,或者使用了一般 catch 子句,如 catch()。
规则说明
不应捕捉一般异常。
如何解决冲突
要修复与该规则的冲突,请捕捉更具体的异常,或者在执行 catch 块中的最后一条语句时重新引发一般异常。
何时禁止显示警告
不要禁止显示此规则发出的警告。 捕捉一般异常类型可以对库用户隐藏运行时问题,并且可以增加调试的复杂程度。
备注
从 .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;
}
}
};
}