当我在这段代码中使用try catch exception时,出现以下错误:



我的代码:

   public System.Drawing.Image Scan()
    {
        try
        {

           const string formatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}";
            WIA.CommonDialog scanDialog = new WIA.CommonDialog();
            WIA.ImageFile imageFile = null;
            imageFile = scanDialog.ShowAcquireImage(WIA.WiaDeviceType.ScannerDeviceType, WIA.WiaImageIntent.GrayscaleIntent,

            WIA.WiaImageBias.MinimizeSize, formatJPEG, false, true, false);
            WIA.Vector vector = imageFile.FileData;
            System.Drawing.Image i = System.Drawing.Image.FromStream(new System.IO.MemoryStream((byte[])vector.get_BinaryData()));

            return i;

        }
        catch (COMException ce)
        {
            if ((uint)ce.ErrorCode == 0x800A03EC)
            {
               return ce;

            }
        }

最佳答案

像下面那样更改您的catch块将起作用,但是仍然遇到一些问题。因为您的方法返回Image类型,并且您在catch块中返回COMException。我建议您抛出异常或登录catch块

if ((uint)ce.ErrorCode == 0x800A03EC)
{
    //DO LOGGING;
}
else
{
    throw ce;
}

10-07 17:27