gpt4 book ai didi

c# - 引发异常会导致TargetInvocationException

转载 作者:行者123 更新时间:2023-12-03 08:53:55 27 4
gpt4 key购买 nike

为什么我得到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'.

最佳答案

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

试试看!

关于c# - 引发异常会导致TargetInvocationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33027886/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com