.net 4.5 - C# How to catch an exception on generic abstract class constructor -


i unable catch exception generic abstract class in client app. reason want exception thrown in constructor config file functionality needs same in derived classes , therefore don't see reason implement in every derived class. exception should handled in overarching generic class agentmodule<t> however, not case reason unaware.

i can catch exception when move code method , invoke 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 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 { { return _name; } }     public override string description { { return _description; } }      private string _name;     private string _description;        public xload()     {         _name = "load";         _description = "some desc";      }      public override void load(ilogger logger)     {         console.writeline("i loading");     } } 

the visual studio debugger not catch exception whatever happens within activator.createinstance(). if execute exe manually/programmatically, exception handled. can exception thrown @ constructor of importer class innerexception @ agentmodule.run()

try     {        _importer = (importer)activator.createinstance<t>();     } catch (exception e)      {         console.writeline(e.message);           if(e.innerexception != null)             console.writeline(e.innerexception.message );       } 

Comments