简体   繁体   中英

C# How to catch an exception on generic abstract class constructor

I am unable to catch an exception from a generic abstract class in the client app. The reason I want the exception to be thrown in the constructor is that the config file functionality needs to be the same in the derived classes and therefore I don't see a reason to implement this in every derived class. The exception should be handled in the overarching generic class AgentModule<T> however, this is not the case for a reason I am unaware.

I can catch the exception when I move the code to a method and invoke from the client class.

The abstract class:

public abstract class Importer
{
    public abstract string Name { get; }
    public abstract string Description { get; }

    private System.Configuration.Configuration _customConfig;

    public Importer()
    {
        string customConfigFile = this.GetType().Assembly.Location + ".config";

        if (System.IO.File.Exists(customConfigFile))
        {
            System.Configuration.ExeConfigurationFileMap fileMap = new System.Configuration.ExeConfigurationFileMap();
            fileMap.ExeConfigFilename = customConfigFile;
            _customConfig = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap, System.Configuration.ConfigurationUserLevel.None);
        }
        else
        {
            throw new System.IO.FileNotFoundException("Could not load configuration file: " + customConfigFile);
        }
    }

    public abstract void Load(ILogger logger);
}

The generic overarching class:

public class AgentModule<T> : ModuleBase where T : Importer, new()
{
    private Importer _importer;

    public override void Run()
    {
        try
        {
            _importer = (Importer)Activator.CreateInstance<T>();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

Derived class:

public class XLoad : Importer
{
    public override string Name { get { return _name; } }
    public override string Description { get { return _description; } }

    private string _name;
    private string _description;  

    public XLoad()
    {
        _name = "Load";
        _description = "some desc";

    }

    public override void Load(ILogger logger)
    {
        Console.WriteLine("I am loading");
    }
}

The Visual Studio debugger will not catch any exception for whatever happens within Activator.CreateInstance(). But if you execute the exe manually/programmatically, this exception will be handled. You can even get the exception thrown at the constructor of the Importer class from the InnerException at AgentModule.Run()

try
    {
       _importer = (Importer)Activator.CreateInstance<T>();
    }
catch (Exception e)
     {
        Console.WriteLine(e.Message);

         if(e.InnerException != null)
            Console.WriteLine(e.InnerException.Message );
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM