我的逻辑层中有一个函数,它通过另一个线程插入信息,我希望如果在该线程中发生未知异常抛出它,但问题是在我的应用程序UnhandledExceptionEventHandler
中获得一般异常会产生此错误我也不想关闭应用程序:
调用线程必须是STA,因为许多UI组件需要此
///////////////////// this is my function in logical layer //////////
public string MahaleInsert(Mahales Mahale)
{
Thread t = new Thread(delegate()
{
try
{
Mahale.OutMessage = DA_Agency.MahaleInsertDB(Mahale);
Mahale.OutMessage = "SuccessInsert";
}
catch (Exception ex)
{
if (ex.Message.ToLower().Contains("violation of unique key constraint"))
Mahale.OutMessage = "MahaleInsUniqueError";
else
throw;
}
});
t.Start();
t.Join();
return Mahale.OutMessage;
}
//////////////////////////// this in my aplication level //////////////////
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
string messsage = GeneralMethod.CatchException(e.ExceptionObject as Exception);
MessageClass.MessageBox(messsage);
}
catch
{
}
}
答案 0 :(得分:1)
尝试执行以下操作:
public string MahaleInsert(Mahales Mahale)
{
Thread t = new Thread(new ThreadStart(ThreadBody));
t.Start();
t.Join();
return Mahale.OutMessage;
}
[STAThread]
void ThreadBody()
{
try
{
Mahale.OutMessage = DA_Agency.MahaleInsertDB(Mahale);
Mahale.OutMessage = "SuccessInsert";
}
catch (Exception ex)
{
if (ex.Message.ToLower().Contains("violation of unique key constraint"))
Mahale.OutMessage = "MahaleInsUniqueError";
else
throw;
}
}
//////////////////////////// this in my aplication level //////////////////
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
string messsage = GeneralMethod.CatchException(e.ExceptionObject as Exception);
MessageClass.MessageBox(messsage);
}
catch
{
}
}