c# - try catch 异常错误

标签 c# .net error-handling

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

"not all code paths return values"



我的代码:
   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;
}

关于c# - try catch 异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26125292/

相关文章:

c# - Entity Framework 默认日期时间注释不迁移

c# - Windows 窗体应用程序中的 HTML 编辑器

c# - LINQ 中的最大日期记录

unit-testing - Grails:使用 spock 测试约束错误消息

c# - MEF Composition ImportMany 产生同一类的两个版本

c# - 在 WPF 应用程序中更改默认打印机

c# - 授权属性在 MVC 4 中不起作用

c# - 在卷纸上打印

javascript - 当 check() 抛出 Match.Error 时如何向客户端抛出错误消息?

node.js - 使用 nodejs bunyan 正确记录错误?