hemtros
7/4/2015 - 4:47 AM

Exception Bubbling up to the method that first called method in a chain

Exception Bubbling up to the method that first called method in a chain

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

namespace ExceptionHandling
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                MethodA();
            }
            catch (Exception ex)    // catches exception thats passed by methodB to methodA to Main
            {
                
                Console.WriteLine(ex.StackTrace);
            }
            Console.Read();
        }

        static void MethodA()
        {
            try
            {
                MethodB();
            }
            catch (Exception)   // this exception was thrown by MethodB
            {
                
                throw;      //throws catched exception that bubbles up to the method that called this method.
            }
        }

        static void MethodB()
        {
            try
            {
                throw new Exception();        //raises exception
            }
            catch (Exception)
            {
                
                throw;   //again throws exception but bubbles up to the method that called it.
            }
        }
    }
}