Exception Propagation को C# में Exception Handling कैसे काम करता है, उससे समझा जा सकता है।
Try में, जब Exception Occur होता है तो Corresponding Catch Blocks Check किए जाते हैं। यह यह देखने के लिए किया जाता है कि क्या वे Exception Catch कर सकते हैं। यदि कोई Matching Exception नहीं मिलता, तो Exception Higher-Level Try Block में Propagate होता है। यह तब तक Repeat होता है जब तक Exception Catch नहीं हो जाता। यदि Exception Catch नहीं होता, तो Program का Execution End हो जाता है।
ऊपर दिया Concept नीचे दिए Example में Nested Try Statements के साथ Explain किया गया है।
Example
using System; using System.Text; public class Demo { public static void Main() { try { try { throw new ArgumentException(); } catch (NullReferenceException e) { Console.WriteLine("catch one"); } finally { Console.WriteLine("finally one"); } } catch (Exception e) { Console.WriteLine("catch two"); } finally { Console.WriteLine("finally two"); } Console.ReadLine(); } }Output
finally one catch two finally two