C# 中的链式异常

C# 中的链式异常

链接异常是一系列处理异常的try-catch语句。要创建异常链,即链接异常−

设置第一个try-catch −

示例

static void Main(string[] args) { try { One(); } catch (Exception e) { Console.WriteLine(e); } }登录后复制

示例

static void One() { try { Two(); } catch (Exception e) { throw new Exception("First exception!", e); } }登录后复制

示例

static void Two() { try { Three(); } catch (Exception e) { throw new Exception("Second Exception!", e); } }登录后复制

示例

static void Three() { try { Last(); } catch (Exception e) { throw new Exception("Third Exception!", e); } }登录后复制

Example

static void Last() { throw new Exception("Last exception!"); }登录后复制

System.Exception: First exception! ---< System.Exception: Middle Exception! ---< System.Exception: Last exception! at Demo.Two () [0x00000] in :0 --- End of inner exception stack trace --- at Demo.Two () [0x00016] in :0 at Demo.One () [0x00000] in :0 --- End of inner exception stack trace --- at Demo.One () [0x00016] in :0 at Demo.Main (System.String[] args) [0x00000] in :0登录后复制