Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
控制台应用程序中System.Xml的C#错误处理_C#_Error Handling_Unhandled Exception - Fatal编程技术网

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

控制台应用程序中System.Xml的C#错误处理,c#,error-handling,unhandled-exception,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

我继承了一个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
未处理的异常:System.Xml.XmlException:分析EntityName时出错。第5行,位置338。
位于System.Xml.XmlTextReaderImpl.Throw(异常e)
位于System.Xml.XmlTextReaderImpl.HandleEntityReference(布尔值为isInAttributeValue、EntityExpandType、Int32和charRefEndPos)
位于System.Xml.XmlTextReaderImpl.ParseText(Int32&startPos、Int32&endPos、Int32&outOrChars)
位于System.Xml.XmlTextReaderImpl.FinishPartialValue()处
在System.Xml.XmlTextReaderImpl.get_Value()处
位于System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r)
位于System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r,LoadOptions o)
位于System.Xml.Linq.XDocument.Load(XmlReader阅读器,LoadOptions)
位于System.Xml.Linq.XDocument.Load(字符串uri,LoadOptions)
位于System.Xml.Linq.XDocument.Load(字符串uri)
在C:\Users\default\Source\Workspaces\Data\Data-Transfer\Data.Service\AssignmentService.cs中的Data.Service.AssignmentService.d\uu 10.MoveNext()处:第381行
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在C:\Users\default\Source\workspace\Data\Data-Transfer\Data.Service\AssignmentService.d_uu17.MoveNext()中的Vital4Data.Service.AssignmentService.d_u17.MoveNext()处:第948行
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.CompilerServices.AsyncMethodBuilderCore.c.b_u6_u1(对象状态)
位于System.Threading.QueueUserWorkItemCallback.WaitCallback_上下文(对象状态)
位于System.Threading.ExecutionContext.RunInternal(ExecutionContext ExecutionContext、ContextCallback回调、对象状态、布尔值preserveSyncCtx)
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔保存SyncCTX)
位于System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()处
在System.Threading.ThreadPoolWorkQueue.Dispatch()中
在System.Threading.\u ThreadPoolWaitCallback.PerformWaitCallback()中
状态更改为失败
由于退出代码-532462766,作业失败

在我能让某人深入应用程序来解决这个问题之前,我只需要应用程序忽略这个错误,并且暂时不失败地继续。我将如何向代码中添加错误处理来实现这一点?这是一个try/catch块的候选对象吗?或者,是一个try/catch块首先捕获并抛出了这个?我非常感谢任何指导,这样我就可以简单地告诉程序忽略错误并继续执行。

控制台应用程序通常用于计划的作业。
正如ColinM所说,您应该实现一个try-catch,但我也建议您记录您的跟踪以作进一步分析。

只需添加一个带有空catch子句的try-catch。我只需要应用程序忽略此错误,继续而不失败是非常幼稚和无知的做法。调查XML导致代码出错的原因。打开有问题的Xml文件未经处理的异常:System.Xml.XmlException:解析EntityName时出错。第5行,位置338。缺少错误处理是一个灾难的处方,使用<代码> XMLExcExabor >代码>在<代码> catch <代码>块中确定它是什么,而不是使用泛型<代码>异常< /代码>这是一个非常糟糕的代码气味。@ T0MM13B我完全理解,但这是非常临时的创可贴(字面上最多是5天)。直到我有一个专门的全职资源,谁可以做你提到的事情。谢谢你提供的信息,是的,我绝对计划在下周收到我的新资源时进一步分析。关于实现try/catch,第381行的代码是switch语句中的一个案例-我需要在try/catch中包装整个switch语句,还是只包装第381行的这一个案例?既然你说你继承了应用程序,我将包装整个函数,而不仅仅是开关。你可能不知道这个应用程序有多像“潘多拉盒子”,所以我现在不会冒任何风险。然后,当新资源到达时,最好重构代码。干杯