控制台应用程序中System.Xml的C#错误处理

时间:2017-06-06 21:29:05

标签: c# error-handling unhandled-exception

我继承了一个C#控制台应用程序,而且我并不精通编写错误处理。经过几个月的这个应用程序每晚运行没有任何问题,它只是开始每天,在过去3天内抛出以下错误:

 Unhandled Exception: System.Xml.XmlException: An error occurred while parsing EntityName. Line 5, position 338.
    at System.Xml.XmlTextReaderImpl.Throw(Exception e)
    at System.Xml.XmlTextReaderImpl.HandleEntityReference(Boolean isInAttributeValue, EntityExpandType expandType, Int32& charRefEndPos)
    at System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos, Int32& endPos, Int32& outOrChars)
    at System.Xml.XmlTextReaderImpl.FinishPartialValue()
    at System.Xml.XmlTextReaderImpl.get_Value()
    at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r)
    at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o)
    at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
    at System.Xml.Linq.XDocument.Load(String uri, LoadOptions options)
    at System.Xml.Linq.XDocument.Load(String uri)
    at Data.Service.AssignmentService.<HandleMarkupActivityStep>d__10.MoveNext() in C:\Users\default\Source\Workspaces\Data\Data - Transfer\Data.Service\AssignmentService.cs:line 381
 --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at Vital4Data.Service.AssignmentService.<ProcessSingleHtmlSource>d__17.MoveNext() in C:\Users\default\Source\Workspaces\Data\Data - Transfer\Data.Service\AssignmentService.cs:line 948
 --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__6_1(Object state)
    at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
    at System.Threading.ThreadPoolWorkQueue.Dispatch()
    at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
 Status changed to Failed
 Job failed due to exit code -532462766

在我可以让某人深入了解应用程序以解决问题之前,我只需要应用程序忽略此错误,并立即继续,而不会失败。我将如何在代码中添加错误处理来实现此目的?这是try / catch块的候选者吗?或者是一个尝试/捕获块首先抓住并扔掉了什么?我很欣赏任何指导,所以我可以简单地告诉程序忽略错误并继续执行。

1 个答案:

答案 0 :(得分:1)

控制台应用通常用于预定作业。 正如ColinM所说,你应该实现try-catch,但我也建议你记录你的跟踪以便进一步分析。