我一直在编写简单的控制台应用程序来学习 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 在调试模式下处理不同类型的异常是否有所不同?