Effective exception handling methods

I am writing a C # application that requires me to create an object Array"on the fly" from some measurements that the user goes through. The method Array.CreateInstance()can raise (ultimately) 6 different exceptions that I would like to handle. For each exception, I would like to inform the user with a simple MessageBox.Show()message and appropriate exceptional circumstances. What I do not want to do is to catch the general type Exception, because it is better not to do this. I would try to catch ArgumentExceptionor something more specific, but the only common superclass for all exceptions is Exception.

Bottom Line . I am trying to find the best way to handle many different exceptions and what will be an effective and, more importantly, service-friendly solution.

try
{
    data = Array.CreateInstance(TypeHelper.StringToType(cbDataType.SelectedItem.ToString()), dimensions);
}
catch (OutOfMemoryException) { }
catch (NullReferenceException) { }
catch (NotSupportedException) { }
catch (ArgumentNullException) { }
catch (ArgumentOutOfRangeException) { }
catch (ArgumentException) { }
+3
source share
4 answers

There are only 4 exceptions to this list that I would consider:

  • NotSupportedException
  • ArgumentNullException
  • ArgumentOutOfRangeException
  • ArgumentException

The other two that you should never catch, and on a later CLR, you cannot catch the situation with OOM (consider MemoryFailPoint if you need to find out).

Going deeper into Array.CreateInstance, we see why each of these four will be cast:

  • NotImplementedException: , , . , , . .
  • ArgumentNullException: , , , , , .
  • ArgumentOutOfRangeException: 0, , .
  • ArgumentException: , ( , ) , .

, :

// code prior to this point ensures cbDataType only has correct types
// and dimensions has at least 1 dimension and is all greater than or equal to 1
data = Array.CreateInstance(
    TypeHelper.StringToType(cbDataType.SelectedItem.ToString()),
    dimensions);

, - , , , .

+11

Array.CreateInstance, , - , . , ArgumentNullException , , , NotSupportedException , , , , . , , , , .

, , -, , - OutOfMemoryException, , , .

+2

, , :

// somewhere ...
public static IDictionary<Type, string> exceptionMessages;

// in your method ...
try { ... }
catch( Exception ex ) {        
    var exType = ex.GetType();
    if( exceptionMessages.ContainsKey(exType) ) {
        MessageBox.Show( exceptionMessages[exType] );
    }
    else {
        throw ex;
    }
}

, - , , , , .

, , Action < > delegates... , .;)

0

- , Array.CreateInstance, , .

However, if there are several types of exceptions that you want to catch, you will have to use reflection to simplify the code. Unfortunately (or maybe lucky) catchblocks are not counted among many C # constructors that can share area blocks.

try
{
}
catch (Exception caught)
{
    Type[] types = 
      { 
        typeof(OutOfMemoryException), 
        typeof(NullReferenceException) 
        // Continue adding exceptions to be filtered here.
      };
    if (types.Contains(caught.GetType()))
    {
        // Handle accordingly.
    }
    else
    {
        throw; // Rethrow the exception and preserve stack trace.
    }
}
0
source

Source: https://habr.com/ru/post/1753572/


All Articles