简体   繁体   English

C# CryptographicException 未被捕获

[英]C# CryptographicException not being caught

I have a serialisable class called DataSet which has a static method Load(string filename, string password) which returns the deserialised DataSet.我有一个名为 DataSet 的可序列化 class,它有一个 static 方法Load(string filename, string password) ,它返回反序列化的 DataSet。

Here it is:这里是:

public static DataSet Load(string filename, string password)
{
  if (!File.Exists(filename))
    throw new FileNotFoundException("File not found.", filename);

  DataSet ds;

  ICryptoTransform ct = Encryption.getDecryptor(password, salt, iv);

  using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
  {
    using (CryptoStream cs = new CryptoStream(fs, ct, CryptoStreamMode.Read))
    {
      using (GZipStream zs = new GZipStream(cs, CompressionMode.Decompress))
      {
        try
        {
          ds = (DataSet)new BinaryFormatter().Deserialize(zs);
          return ds;
        }
        catch
        {
          throw new ApplicationException("This password cannot be used to decrypt this file. Either the password is incorrect or the file is corrupt");
        }
        finally
        {
          zs.Close();
        }
      }
    }
  }
}

And I'm calling it like this:我这样称呼它:

try
{
  dataSet = DataSet.Load(ofd.FileName, ep.Password);
}
catch (ApplicationException ae)
{
  MessageBox.Show("Error:\r\n" + ae.Message, "Authorisation Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
}

With the correct password, it works fine.使用正确的密码,它可以正常工作。 I'm testing it with the incorrect password.我正在使用错误的密码对其进行测试。 The expected result is for a MessageBox to pop up saying "This password cannot be used to decrypt this file [...]".预期的结果是消息框弹出“此密码不能用于解密此文件 [...]”。 Instead, what happens is I get an uncaught exception window.相反,我得到了一个未捕获的异常 window。

If I'm debugging in VS, I can see that an uncaught CryptographicException occurred.如果我在 VS 中调试,我可以看到发生了未捕获的 CryptographicException。 I originally had a try/catch with 2 catches, one for CryptographicException and one for SerializationException.我最初有一个带有 2 个捕获的 try/catch,一个用于 CryptographicException,一个用于 SerializationException。 That didn't work.那没有用。 I replaced it to catch Exception.我将其替换为捕获异常。 Finally, I have a catch all.最后,我有一个全部。

I don't know why, but for some reason it cannot seem to catch this?我不知道为什么,但由于某种原因,它似乎无法捕捉到这个? I'm sure the answer is very obvious but I just can't see it.我确信答案很明显,但我就是看不到。

I know some exceptions are uncatchable such as StackoverflowException.我知道有些异常是无法捕获的,例如 StackoverflowException。 I suspect CryptographicException is not uncatchable.我怀疑 CryptographicException 不是无法捕获的。

Why are you catching with ApplicationException?你为什么要赶上 ApplicationException? If you catch with Exception you should catch the exception.如果您使用异常捕获,则应该捕获异常。

Given your comment, it looks like the exception is being thrown in one of the stream constructors.鉴于您的评论,似乎在 stream 构造函数之一中引发了异常。 If you place your try catch around more of the code, you will catch it ok.如果你把你的 try catch 放在更多的代码上,你会抓住它的。

The reason that your ApplicationException is not "taking precedence" is because the CryptographicException is being thrown from outside your try/catch block.您的ApplicationException不是“优先”的原因是因为CryptographicException是从您的try/catch之外抛出的。

In other words, Deserialize is not the only API that can throw a CryptographicException .换句话说, Deserialize不是唯一可以抛出CryptographicException的 API 。 You simply need to enlarge your try/catch/finally block to encompass all API calls that might throw an exception.您只需扩大您的try/catch/finally块以包含所有可能引发异常的 API 调用。 After you have done this, ApplicationException will be the only possible exception that Load can throw and your code should work as expected.完成此操作后, ApplicationException将是Load可以抛出的唯一可能的异常,并且您的代码应该按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM