try catch捕获异常

参考地址:https://www.cnblogs.com/OpenCoder/p/6208284.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExceptionTest2
{
class Program
{
static void ThrowExceptionFunction()
{
try
{
try
{
try
{
throw new Exception("模拟异常");
}
catch (Exception ex1)
{
throw;
}
}
catch (Exception ex2)
{
throw;
}
}
catch (Exception ex3)
{
throw;
}

}


static void Main(string[] args)
{
try
{
ThrowExceptionFunction();
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);//因为C#会为每个函数的异常记录一次堆栈信息,而本例中有两个函数分别为ThrowExceptionFunction和Main,所以这里堆栈捕捉到了两个异常一个是在函数ThrowExceptionFunction中32行,另一个是Main函数中42行,
}

Console.ReadLine();
}
}
}

原文地址:https://www.cnblogs.com/kingsmart/p/13439647.html