Suppose I want to validate some arguments for a constructor of an object to be used later. Also, suppose the criteria for validation of these arguments is elaborate.
For example, I want to check that some items I need to create an X509Certificate2 are legitimate. I've been calling the constructor inside a try{} and catching exceptions, but static analysis (correctly) reports "CA1804 remove unused locals A method declares a local variable but does not use the variable except possibly as the recipient of an assignment statement."
Consider this code:
string password = "bosco";
byte[] certyBytes = new byte[]{1,2,3};
try
{
X509Certificate2 tpCert = new X509Certificate2(certBytes, password);
}
catch (CryptographicException)
{
log.Debug("Bad cert.");
}
Static analysis reports CA1804 on line 5 above because of the unused tpCert.
Is there a better way to do this?