我一直在写一个简单的控制台应用程序来学习C#中的异常。在我的代码中,我有简单的
try-catch
块,
NullReferenceException
,Visual Studio会跳转并显示异常,就好像该行中有断点一样,然后我可以手动继续执行。
但当程序中存在另一种异常时,比如
ArgumentNullException
,程序正常执行并完成。
请考虑以下代码:
string[] myArray = {null};
try
{
Console.WriteLine(myArray[0].GetType());
}
catch (NullReferenceException x)
{
Console.WriteLine(x.Message);
}
在上面的代码中,当
NullReferenceException
发生时,它会从控制台窗口跳到Visual Studio。
但在下面的代码中,它正常执行并停留在控制台窗口上:
string[] myArray = {null};
try
{
Console.WriteLine(Int32.Parse(myArray[0]));
}
catch (ArgumentNullException x)
{
Console.WriteLine(x.Message);
}
Visual Studio在调试模式下是否以不同的方式处理不同类型的异常?