Three Level Exception Catch - CSharp Custom Type

CSharp examples for Custom Type:try catch finally

Description

Three Level Exception Catch

Demo Code

using System;//  w  w  w.ja v  a  2  s. c  o m
using System.ComponentModel;
using System.IO;
#pragma warning disable CS0168 // Variable is declared but never used
class ThreeLevelException
{
   static bool LogAndReturn(string message, bool result)
   {
      Console.WriteLine(message);
      return result;
   }
   static void Top()
   {
      try
      {
         throw new Exception();
      }
      finally
      {
         Console.WriteLine("Top finally");
      }
   }
   static void Middle()
   {
      try
      {
         Top();
      }
      catch (Exception e) when (LogAndReturn("Middle filter", false))
      {
         Console.WriteLine("Caught in middle");
      }
      finally
      {
         Console.WriteLine("Middle finally");
      }
   }
   static void Bottom()
   {
      try
      {
         Middle();
      }
      catch (IOException e) when (LogAndReturn("Never called", true))
      {
       }
       catch (Exception e) when (LogAndReturn("Bottom filter", true))
       {
          Console.WriteLine("Caught in Bottom");
       }
    }
    static void Main()
    {
       Bottom();
    }
}

Result


Related Tutorials