我尝试使用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.UnhandledException
和App.Current.DispatcherUnhandledException
CTRL+ALT+E
)并取消选中Thrown
TagLib
类我还应该注意,上面的代码是在一个单独的线程中执行而不是WPF的主要STA线程,并且当我不在Visual Studio Debugger下运行代码时,没有错误。还有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:
答案 0 :(得分:0)
在Visual Studio中,转到菜单Debug - >例外。然后在说“"公共语言运行时异常"取消选中" Thrown"柱。这将在抛出异常时阻止调试器中断。
答案 1 :(得分:0)
您是否尝试将异常标记为已处理? 在catch块中,使用异常的ExceptionHandled属性并将其设置为true。
根据异常的类型,属性名称可以是“已处理”或“ExceptionHandled”。
你可以这样做
ex.handled = true;
答案 2 :(得分:0)
似乎问题是Visual Studio调试器与外部代码不兼容。调用System.Activator.CreateInstance()
时,它会运行一些外部代码,以便为运行时指定的类调用构造函数。这似乎导致Visual Studio调试器忽略以前的try-catch块(无论出于何种原因)。当我直接调用类的构造函数(不使用System.Activator
或System.Reflection
)时,使用try-catch块捕获异常没问题。
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;
}
我希望这有助于将来可能遇到此问题的其他人!