1.异常来源
2.代码演示
using System;
using System.Reflection.Metadata;
using System;
using System.Collections.Generic;
class MainClass
{
static void Main(string[] args)
{
try
{
List<string> data = new List<string>() { "123", "456" };
Console.WriteLine(data[3]);
}
catch (ArgumentOutOfRangeException)
{
Console.WriteLine("数组越界");
}
catch (Exception ex)
{
Console.WriteLine("其他错误"+ex);
}
}
}
3.抛出异常方式区别
(1) throw;直接抛出异常,会保留原始异常点信息;
(2) throw ex;会吞掉原始异常点信息,重置堆栈中的异常起始点。
(3) throw new Exception("异常描述",ex); 包装一个异常,把内部异常Exception也抛出来,这样抛出的异常是最全面详细的异常。经过对异常重新包装,但是会保留原始异常点信息
结论:推荐使用第三种