首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >抛出异常会导致TargetInvocationException

抛出异常会导致TargetInvocationException
EN

Stack Overflow用户
提问于 2015-10-09 08:04:42
回答 1查看 1K关注 0票数 0

为什么我会得到一个System.Reflection.TargetInvocationException而不是我抛出的异常?

我的windows程序是

代码语言:javascript
复制
        try
        {
            winApplication.Setup();
            winApplication.Start();
        }
        catch (ExceptionDatabaseVersion e)
        { 
          // ask whether to upgrade the database
        }
        catch (Exception e)
        {
           MessageBox.Show( e.ToString()); // displays TargetInvocationException.  Why?

         }

我的错误被定义为

代码语言:javascript
复制
[Serializable()]
public class ExceptionDatabaseVersion : Exception
{
    public ExceptionDatabaseVersion(string message, Exception inner)
        : base(message, inner)
    {

    }

    public ExceptionDatabaseVersion(string message)
        : base(message )
    {

    }
}

我的错误在这里提出

代码语言:javascript
复制
    public MyDbContext(string connectionString)
        : base(connectionString)
    {
        if (!Database.Exists())
        {
            Database.SetInitializer(new CreateInitializer());
        }
        else
        {
            bool isCompatible = false;
            try
            {
                isCompatible = Database.CompatibleWithModel(false);
            }
            catch (Exception ex)
            {
                throw new ExceptionDatabaseVersion("Data structure changed", ex);
            }
            if (!isCompatible)
            {
                // Here is where it gets thrown.  
                throw new ExceptionDatabaseVersion("Data structure changed");
            }
        }
    }

错误是

代码语言:javascript
复制
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Solution1.Module.BusinessObjects.ExceptionDatabaseVersion: my message
   at Solution1.Module.BusinessObjects.MyDbContext..ctor(String connectionString) in e:\EShared\Dev2015\DXSupportSolution1\Solution1.Module\BusinessObjects\MyDbContext.cs:line 35
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.Activator.CreateInstance(Type type, Object[] args)
   at DevExpress.ExpressApp.EF.EFObjectSpaceProvider.CreateObjectContext(IList`1 disposableObjects)
   at DevExpress.ExpressApp.EF.EFObjectSpaceProvider.Init(Type contextType, ITypesInfo typesInfo, EFTypeInfoSource efTypeInfoSource, DbConnection connection, String connectionString, String metadataLocations, String providerName)
   at DevExpress.ExpressApp.EF.EFObjectSpaceProvider..ctor(Type contextType, ITypesInfo typesInfo, EFTypeInfoSource efTypeInfoSource, String connectionString, String metadataLocations, String providerName)
   at DevExpress.ExpressApp.EF.EFObjectSpaceProvider..ctor(Type contextType, ITypesInfo typesInfo, EFTypeInfoSource efTypeInfoSource, String connectionString)
   at Solution1.Win.Solution1WindowsFormsApplication.CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) in e:\EShared\Dev2015\DXSupportSolution1\Solution1.Win\WinApplication.cs:line 22
   at DevExpress.ExpressApp.XafApplication.CreateObjectSpaceProviders(String connectionString)
   at DevExpress.ExpressApp.XafApplication.Setup()
   at Solution1.Win.Program.Main() in e:\EShared\Dev2015\DXSupportSolution1\Solution1.Win\Program.cs:line 42
'Solution1.Win.exe' (CLR v4.0.30319: Solution1.Win.exe): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\12.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll'. 
EN

回答 1

Stack Overflow用户

发布于 2015-10-09 08:43:36

代码语言:javascript
复制
public MyDbContext(string connectionString)
    : base(connectionString)
{
    if (!Database.Exists())
    {
        Database.SetInitializer(new CreateInitializer());
    }
    else
    {
        bool isCompatible = false;

        isCompatible = Database.CompatibleWithModel(false);


        if (!isCompatible)
        {
            // Here is where it gets thrown.  
            throw new ExceptionDatabaseVersion("Data structure changed");
        }
    }
}` 

试试这个吧!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33027886

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档