未捕获异常

本文关键字:捕获异常 | 更新日期: 2023-09-27 18:21:43

在我的MVC 5应用程序中,我有以下代码:

try
{
    var inputFile = new MediaFile { Filename = videoPath };
    var outputFile = new MediaFile { Filename = Config.ThumbPath };
    using (var engine = new Engine())
    {
        engine.GetMetadata(inputFile);
        if (inputFile.Metadata != null && inputFile.Metadata.Duration.TotalSeconds > 0)
        {
            var options = new ConversionOptions { Seek = TimeSpan.FromSeconds(inputFile.Metadata.Duration.TotalSeconds / 2) };
            engine.GetThumbnail(inputFile, outputFile, options);
            byte[] imageArray = System.IO.File.ReadAllBytes(Config.ThumbPath);
            base64ImageRepresentation = "data:image/jpeg;base64," + Convert.ToBase64String(imageArray);
        }
    }
}
catch (Exception ex)
{ 
     if (!EventLog.SourceExists("MYAPP"))
           EventLog.CreateEventSource("MYAPP", "Application");
     EventLog.WriteEntry("MYAPP", "CACHED: " + ex.Message);
     EventLog.WriteEntry("MYAPP", "CACHED: " + ex.Message,EventLogEntryType.Warning,234);
}

engine.GetMetadata(inputFile);行抛出:

mscorlib.dll 中发生"System.FormatException"类型的未处理异常

它在一个名为MediaToolkit的库中,我从NuGet安装了它。

为什么我的try-catch没有捕捉到这个异常?

它得到的文件已损坏,这就是它抛出错误的原因,但如果发生这种情况,我只想继续到下一个文件。但是,应用程序却停止了。

事件日志中没有此项,但源ASP.NET有另一项。

An unhandled exception occurred and the process was terminated.
Application ID: /LM/W3SVC/2/ROOT
Process ID: 7364
Exception: System.FormatException
Message: Input string was not in a correct format.
StackTrace:    at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
   at System.Convert.ToInt32(String value)
   at MediaToolkit.Util.RegexEngine.TestAudio(String data, EngineParameters engine)
   at MediaToolkit.Engine.<>c__DisplayClass9.<FFmpegEngine>b__5(Object sender, DataReceivedEventArgs received)
   at System.Diagnostics.Process.ErrorReadNotifyUser(String data)
   at System.Diagnostics.AsyncStreamReader.FlushMessageQueue()
   at System.Diagnostics.AsyncStreamReader.GetLinesFromStringBuilder()
   at System.Diagnostics.AsyncStreamReader.ReadBuffer(IAsyncResult ar)
   at System.IO.Stream.ReadWriteTask.InvokeAsyncCallback(Object completedTask)
   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.IO.Stream.ReadWriteTask.System.Threading.Tasks.ITaskCompletionAction.Invoke(Task completingTask)
   at System.Threading.Tasks.Task.FinishContinuations()
   at System.Threading.Tasks.Task.FinishStageThree()
   at System.Threading.Tasks.Task.FinishStageTwo()
   at System.Threading.Tasks.Task.Finish(Boolean bUserDelegateExecuted)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
   at System.Threading.Tasks.Task.ExecuteEntry(Boolean bPreventDoubleExecution)
   at System.Threading.Tasks.Task.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

未捕获异常

您的Visual Studio可能已配置为停止对每个生成的异常执行(甚至已处理)。您可以在调试->异常中进行检查。检查公共语言运行时异常类别,抛出设置。如果选中此设置,VS将在每次生成CLR异常时停止执行,您只需按F5键即可继续或关闭此设置。

有关更多信息,请访问MSDN。