【问题标题】:Try-catch block showing as not being handledTry-catch 块显示为未处理
【发布时间】:2026-01-28 09:15:02
【问题描述】:

我正在尝试使用TagLibSharp 来获取音频文件的标签。我有一个 try-catch 块(如下所示)来捕获库引发的异常,但是,当我在 Visual Studio 2013 调试器下运行代码时,它会说这是第一次机会异常并且未处理。如何让 Visual Studio 2013 在调试器下运行时不停止?

                TagLib.File file = null;
                try
                {
                    file = TagLib.File.Create(this.FilePath);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("The following exception occurred: {0}", ex.Message);
                }

我尝试了以下方法,但它们也没有奏效:

  • AppDomain.CurrentDomain.UnhandledExceptionApp.Current.DispatcherUnhandledException 添加了事件处理程序
  • 在 Visual Studio 中进入异常...(通过按 CTRL+ALT+E)并取消选中 Thrown 下的所有框
  • 在包含 TagLib 类的 DLL 中添加 try-catch 块
  • 清理和重建 Visual Studio 解决方案

我还应该注意,上面的代码是在一个单独的线程中执行的,而不是 WPF 的主 STA 线程,并且当我不在 Visual Studio 调试器下运行代码时,没有错误。还有Activator.CreateInstance 被调用来调用抛出异常的类。 TagLib 抛出的异常是CorruptFileException(即shown here),并且是在调用TagLib.Aiff.File.Read() 时(as shown here)。

按照要求,这是异常详细信息:

TagLib.CorruptFileException was unhandled by user code
  HResult=-2146233088
  Message=File does not begin with AIFF identifier
  Source=TagLib
  StackTrace:
       at TagLib.Aiff.File.Read(Boolean read_tags, ReadStyle style, UInt32& aiff_size, Int64& tag_start, Int64& tag_end) in h:\Users\Nick\Documents\Visual Studio 2013\Projects\xxxxx\Source code\TagLib\Aiff\File.cs:line 407
       at TagLib.Aiff.File..ctor(IFileAbstraction abstraction, ReadStyle propertiesStyle) in h:\Users\Nick\Documents\Visual Studio 2013\Projects\xxxxx\Source code\TagLib\Aiff\File.cs:line 166
  InnerException: 

【问题讨论】:

  • 您是否尝试过将解决方案重建为干净的解决方案?
  • @Elias 是的,我有。我也将它添加到问题中。
  • 能否请您在帖子中包含确切的错误消息?
  • 你有多个App域吗?或者您知道您使用的库是否有非托管代码?三是在跨越应用程序域/非托管代码边界时中断的调试器选项。
  • 如果一些非托管代码抛出异常是可以逃避捕获。我忘记了我必须设置的内容,但我遇到了这种情况。

标签: c# wpf multithreading visual-studio try-catch


【解决方案1】:

在 Visual Studio 中,转到菜单调试 -> 异常。然后在显示“公共语言运行时异常”的行上,取消选中“抛出”列中的框。这将阻止调试器在抛出异常时中断。

【讨论】:

  • 您还需要在调试器设置中启用“仅我的代码”。
  • 正如问题中所述,我已经尝试过了,但它仍然说它未处理。 @ScottChamberlain“只是我的代码”在调试器设置中被选中。
【解决方案2】:

您是否尝试将异常标记为已处理? 在 catch 块中使用异常的 ExceptionHandled 属性并将其设置为 true。

根据异常的类型,属性名称可能是“handled”或“ExceptionHandled”。

你可以这样做

ex.handled = true;

【讨论】:

  • 是的,我将Handled 设置为true
  • 异常没有“已处理”成员。这里到底设置了什么?
  • @HansPassant System.Windows.Threading.DispatcherUnhandledExceptionEventArgs.Handled 是我认为他指的,但我猜他指的是Exception 本身(在这种情况下它没有处理的属性)
【解决方案3】:

问题似乎在于 Visual Studio 调试器不能很好地处理外部代码。当System.Activator.CreateInstance() 被调用时,它会运行一些外部代码以调用运行时指定类的构造函数。这似乎导致 Visual Studio 调试器忽略以前的 try-catch 块(无论出于何种原因)。当我直接调用类的构造函数时(不使用System.ActivatorSystem.Reflection),使用try-catch 块捕获异常没有问题。

有问题的代码(来自 TagLibSharp)

    public static File Create (IFileAbstraction abstraction,
                               string mimetype,
                               ReadStyle propertiesStyle)
    {
        if(mimetype == null) {
            string ext = String.Empty;

            int index = abstraction.Name.LastIndexOf (".") + 1;

            if(index >= 1 && index < abstraction.Name.Length)
                ext = abstraction.Name.Substring (index,
                    abstraction.Name.Length - index);

            mimetype = "taglib/" + ext.ToLower(
                CultureInfo.InvariantCulture);
        }

        foreach (FileTypeResolver resolver in file_type_resolvers) {
            File file = resolver(abstraction, mimetype,
                propertiesStyle);

            if(file != null)
                return file;
        }

        if (!FileTypes.AvailableTypes.ContainsKey(mimetype))
            throw new UnsupportedFormatException (
                String.Format (
                    CultureInfo.InvariantCulture,
                    "{0} ({1})",
                    abstraction.Name,
                    mimetype));

        Type file_type = FileTypes.AvailableTypes[mimetype];

        try
        {
            // This caused the previous try-catch block(s) to be ignored by the Visual Studio debugger
            File file = (File)Activator.CreateInstance(
                file_type,
                new object[] { abstraction, propertiesStyle });

            file.MimeType = mimetype;
            return file;
        } catch (System.Reflection.TargetInvocationException e) {
            throw e.InnerException;
        }
    }

我的解决方案

    private TagLib.File GetTags()
    {
        List<string> validAudioFiles = new List<string>() {
            "aac",
            "aif",
            "ape",
            "wma",
            "aa",
            "aax",
            "flac",
            "mka",
            "mpc",
            "mp+",
            "mpp",
            "mp4",
            "m4a",
            "ogg",
            "oga",
            "wav",
            "wv",
            "mp3",
            "m2a",
            "mp2",
            "mp1"
        };
        TagLib.File file = null;
        string ext = Path.GetExtension(this.FilePath);

        if (!string.IsNullOrEmpty(ext))
        {
            ext = ext.Substring(1).ToLower();

            if (validAudioFiles.Contains(ext))
            {
                try
                {
                    TagLib.File.LocalFileAbstraction abstraction = new TagLib.File.LocalFileAbstraction(this.FilePath);
                    TagLib.ReadStyle propertiesStyle = TagLib.ReadStyle.Average;

                    switch (ext)
                    {
                        case "aac":
                            {
                                file = new TagLib.Aac.File(abstraction, propertiesStyle);
                                break;
                            }
                        case "aif":
                            {
                                file = new TagLib.Aiff.File(abstraction, propertiesStyle);
                                break;
                            }
                        case "ape":
                            {
                                file = new TagLib.Ape.File(abstraction, propertiesStyle);
                                break;
                            }
                        case "wma":
                            {
                                file = new TagLib.Asf.File(abstraction, propertiesStyle);
                                break;
                            }
                        case "aa":
                        case "aax":
                            {
                                file = new TagLib.Audible.File(abstraction, propertiesStyle);
                                break;
                            }
                        case "flac":
                            {
                                file = new TagLib.Flac.File(abstraction, propertiesStyle);
                                break;
                            }
                        case "mka":
                            {
                                file = new TagLib.Matroska.File(abstraction, propertiesStyle);
                                break;
                            }
                        case "mpc":
                        case "mp+":
                        case "mpp":
                            {
                                file = new TagLib.MusePack.File(abstraction, propertiesStyle);
                                break;
                            }
                        case "mp4":
                        case "m4a":
                            {
                                file = new TagLib.Mpeg4.File(abstraction, propertiesStyle);
                                break;
                            }
                        case "ogg":
                        case "oga":
                            {
                                file = new TagLib.Ogg.File(abstraction, propertiesStyle);
                                break;
                            }
                        case "wav":
                            {
                                file = new TagLib.Riff.File(abstraction, propertiesStyle);
                                break;
                            }
                        case "wv":
                            {
                                file = new TagLib.WavPack.File(abstraction, propertiesStyle);
                                break;
                            }
                        case "mp3":
                        case "m2a":
                        case "mp2":
                        case "mp1":
                            {
                                file = new TagLib.Mpeg.AudioFile(abstraction, propertiesStyle);
                                break;
                            }
                    }

                    if (file != null)
                        this._hasAudioTags = true;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("The following exception occurred: " + ex.Message);
                }
            }
        }

        return file;
    }

我希望这对将来可能遇到此问题的其他人有所帮助!

【讨论】: