-1

I need to catch a generic exception and then categorize is based on a specific type to reduce the no of lines of code as all the exception does the same thing .

Something like below

catch (Exception ex)
        {
            Type ExceptionType = ex.GetType();
           switch (ExceptionType.ToString())
            {
                 
                case "IOException":
                case "NullReferenceException":
                system.WriteLine((ExceptionType)ex.Message);
                break;
    }

This shows error there is no type Exception type. Is there a possiblity to try this approach and accomplish this or need to take a typical if else approach. Please help

  • What is the end goal here? If all exception are handled the same way, why do you need to do any categorization? – JonasH Feb 18 '21 at 11:58
  • 1
    `ToString()` will also include the namespace, so it wouldn't match any of those. You'd need `System.IO.IOException` etc., but you're better to filter with the types themselves, than mangle them into strings first. – Martin Costello Feb 18 '21 at 11:59
  • @JonasH The end goal is that based on the exception type say ioexception , the message varies accordingly . Hence this approach is required to make it generic – Dheena Dayalan Feb 18 '21 at 12:03
  • @MartinCostello Could you provide an example ? – Dheena Dayalan Feb 18 '21 at 12:03
  • Use pattern matching `switch(ex){ case IOException: case NullReferenceException: ...` or create a logging function and call it from separate catch blocks – Charlieface Feb 18 '21 at 12:07

2 Answers2

3

Ideally you should handling each Exception individually like so:

try
{
    
}
catch (IOException ex)
{
    // Log specific IO Exception
}
catch (NullReferenceException ex)
{
    // Log Specific Null Reference Exception
}
catch (Exception ex)
{
    // Catch everything else
}

You could do:

    string exceptionErrorMessage;
    
    try
    {
    
    }
    catch (IOException ex)
    {
        // Log specific IO Exception
        exceptionErrorMessage = ex.Message;
    }
    catch (NullReferenceException ex)
    {
        // Log Specific NullReferenceException
        exceptionErrorMessage = ex.Message;
    }
    catch (Exception ex)
    {
        // Catch everything else
        exceptionErrorMessage = ex.Message;
    }
    
    if (!string.IsNullOrEmpty(exceptionErrorMessage))
    {
        // use your logger to log exception.
        Console.WriteLine(exceptionErrorMessage);
    }

Here's the correct to OPs code using the same method he wanted:

try
{

}
catch (Exception e)
{
    var exType = e.GetType().Name;
    switch (exType)
    {
        case "IOException":
        case "NullReferenceException":
            Console.WriteLine(e.Message);
            break;
    }
}
Lee Stevens
  • 472
  • 5
  • 17
  • Better to create a logging function which you call from those specific cases – Charlieface Feb 18 '21 at 12:07
  • @Charlieface, yeah i'd use my logger within each catch for simple answer i just consoled it like he originally did. – Lee Stevens Feb 18 '21 at 12:09
  • @LeeStevens The main idea here is to eliminate the use of multiple different catch block instead use a single catch block and categories type of some specific exception type and print it. The single line of print is used in different catch blocks. I wanted to try if there is a way to not use different catchblock – Dheena Dayalan Feb 18 '21 at 12:21
  • @DheenaDayalan, i have updated my answer to fix your example (at the bottom). That should work fine (haven't tested). – Lee Stevens Feb 18 '21 at 12:28
-1

It sounds like you may be looking for ex.GetType().Name!

In terms of a full solution, that should work with your existing code.

mochsner
  • 307
  • 2
  • 10