抛出异常会导致TargetInvocationException

时间:2015-10-09 00:04:42

标签: c# entity-framework error-handling

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

我的Windows程序是

        try
        {
            winApplication.Setup();
            winApplication.Start();
        }
        catch (ExceptionDatabaseVersion e)
        { 
          // ask whether to upgrade the database
        }
        catch (Exception e)
        {
           MessageBox.Show( e.ToString()); // displays TargetInvocationException.  Why?

         }

我的错误定义为

[Serializable()]
public class ExceptionDatabaseVersion : Exception
{
    public ExceptionDatabaseVersion(string message, Exception inner)
        : base(message, inner)
    {

    }

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

    }
}

我的错误在这里提出

    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");
            }
        }
    }

错误是

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'. 

1 个答案:

答案 0 :(得分:0)

cur.execute("UPDATE student_Data SET attendance = 1 WHERE bluetooth_Id = '%s"%(bluetoothId)+"'")

试试这个!