Sobes.tech
Junior — Senior

Re-throwing exceptions in nested try-catch blocks

livecode

Task condition

There is a C# program in which two nested try-catch blocks rethrow exceptions in different ways. It is necessary to explain the order of execution and show which messages will be displayed.

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

namespace TestProj
{
    static class Program{
        static void Main(string[] args){
            try {
                try {
                    throw new System.Threading.Tasks.TaskSchedulerException();
                } catch {
                    throw ;
                }
            }
            catch (Exception ex) {
                Console.WriteLine(ex);
            }
            try{
                try {
                    throw new IndexOutOfRangeException();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}