【发布时间】:2011-06-05 06:09:53
【问题描述】:
使用 MODI(Microsoft Office Document Imaging)OCR,有时图像不包含任何文本。因此 doc.OCR 会抛出异常。
public static string recognize(string filepath, MODI.MiLANGUAGES language = MODI.MiLANGUAGES.miLANG_RUSSIAN, bool straightenimage = true)
{
if (!File.Exists(filepath)) return "error 1: File does not exist";
MODI.Document doc = new MODI.Document();
doc.Create(filepath);
try
{
doc.OCR(language, false, false);
}
catch
{
//
}
MODI.Image image = (MODI.Image)doc.Images[0];
string result="";
foreach (MODI.Word worditems in image.Layout.Words)
{
result += worditems.Text + ' ';
if (worditems.Text[worditems.Text.Length - 1] == '?') break;
}
doc.Close(false);
System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(doc);
System.Runtime.InteropServices.Marshal.ReleaseComObject(image);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(image);
image = null;
doc = null;
GC.Collect();
GC.WaitForPendingFinalizers();
return result;
}
这段代码终止了应用程序,而不是我需要的:(
我如何让它像什么都没发生一样消失?
【问题讨论】:
-
明确一点——你只是想忽略异常?
-
抛出了什么异常?你确定它是这个方法抛出的吗?
-
该代码看起来只是吞下了异常。我看不出它如何终止应用程序。是不是缺少一些代码?
-
显示的代码不应该终止应用程序 - 所以要么
doc.OCR在内部做了一些相当可怕的事情,要么有一些你没有向我们展示的上下文.. .? -
@Darin,是的,我确定 \\ @Tim,是的
标签: c# exception exception-handling