链接异常是一系列处理异常的try-catch语句。要创建异常链,即链接异常−
设置第一个try-catch −
示例static void main(string[] args) { try { one(); } catch (exception e) { console.writeline(e); }}
现在在方法one()下尝试使用try-catch −
示例static void one() { try { two(); } catch (exception e) { throw new exception("first exception!", e); }}
方法two()还会继续链式异常。
示例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); }}
the takes us to the last.
examplestatic void last() { throw new exception("last exception!");}
在运行上述代码时,异常会被处理如下 −
system.exception: first exception! ---< system.exception: middle exception! ---< system.exception: last exception!at demo.two () [0x00000] in <199744cb72714131b4f5995ddd1a021f>:0--- end of inner exception stack trace ---at demo.two () [0x00016] in <199744cb72714131b4f5995ddd1a021f>:0at demo.one () [0x00000] in <199744cb72714131b4f5995ddd1a021f>:0--- end of inner exception stack trace ---at demo.one () [0x00016] in <199744cb72714131b4f5995ddd1a021f>:0at demo.main (system.string[] args) [0x00000] in <199744cb72714131b4f5995ddd1a021f>:0
以上就是c# 中的链式异常的详细内容。