我已經繼承了一個C#控制檯應用程序,並且我不擅長編寫錯誤處理。幾個月沒有任何問題,夜間運行這個程序後,它剛開始拋出下面的錯誤,是每天對過去的3天:控制檯中的System.Xml的C#錯誤處理應用程序
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塊的候選人嗎?或者是一個嘗試/抓住塊抓住並放在首位?我會很感激任何指導,所以我可以簡單地告訴程序忽略錯誤並繼續執行。
只需添加一個帶有空catch子句的try-catch。 – ColinM
*我只需要應用程序忽略此錯誤,並繼續而不失敗*是非常天真和無知的方法。調查爲什麼XML導致代碼跳過。打開有問題的Xml文件*未處理的異常:System.Xml.XmlException:解析EntityName時發生錯誤。第5行,第338位。*缺少錯誤處理是災難的祕訣,在catch語句塊中使用'XmlException'來確定它是什麼,而不是使用泛型'Exception',這是一種非常糟糕的代碼異味。 – t0mm13b
@ t0mm13b我完全理解,但這是非常臨時的創可貼(最多5天),直到我有一個全職的資源專門爲此人完成您提到的事情。 – Stpete111