简体   繁体   中英

Try-catch block showing as not being handled

I'm trying to use the TagLibSharp to get the tags of audio files. I have a try-catch block (as shown below) to catch exceptions that are thrown by the library, however, when I run the code under the Visual Studio 2013 Debugger, it comes up saying its a first chance exception and is unhandled. How can I make Visual Studio 2013 not stop when its being run under the debugger?

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

I have tried the following but they also didn't work:

  • Added event handlers for both AppDomain.CurrentDomain.UnhandledException and App.Current.DispatcherUnhandledException
  • Going into Exceptions... (by pressing CTRL+ALT+E ) in Visual Studio and unchecking all the boxes under Thrown
  • Adding a try-catch block in the DLL containing the TagLib classes
  • Cleaning and rebuilding the Visual Studio Solution

I should also note that the above code is being executed in a seperate thread than the main STA thread for WPF and that when I don't run the code under the Visual Studio Debugger, there is no error. There is also Activator.CreateInstance being called to call the class that is throwing the exception. The exception that is being thrown by TagLib is CorruptFileException (which is shown here ) and is when TagLib.Aiff.File.Read() is called ( as shown here ).

As asked, here's the exception details:

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: 

In Visual Studio go to the menu Debug -> Exceptions. Then on the line that says "Common Language Runtime Exceptions" uncheck the box in the "Thrown" column. This will stop the debugger from breaking when exceptions are thrown.

Did you try marking the exception as handled? In the catch block make use of the ExceptionHandled property of the exception and set it to true.

Depending on the type of exception the property name may be "handled" or "ExceptionHandled".

You may do it as

ex.handled = true;

It seems the problem is that the Visual Studio debugger doesn't play nice with external code. When System.Activator.CreateInstance() is called, it runs some external code in order to call the constructor for the runtime specified class. This seems to cause the Visual Studio debugger to ignore previous try-catch block(s) (for whatever reason). When I call the constructor for the class directly (without the use of System.Activator or System.Reflection ), the exceptions are caught using the try-catch block no problem.

Problematic Code (From 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;
        }
    }

My Solution

    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;
    }

I hope this helps anybody else that might run into this problem in the future!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM