Exception Filter by exception message - CSharp Custom Type

CSharp examples for Custom Type:Exception

Description

Exception Filter by exception message

Demo Code

using System;// w w w.ja v a2 s. c o  m
using System.ComponentModel;
class FirstExceptionFilter
{
   static void Main()
   {
      string[] messages =
      {
         "You can catch this",
         "You can catch this too",
         "This won't be caught"
      };
      foreach (string message in messages)
      {
         try
         {
            throw new Exception(message);
         }
         catch (Exception e) when (e.Message.Contains("catch"))
         {
            Console.WriteLine($"Caught '{e.Message}'");
         }
      }
   }
}

Result


Related Tutorials