我如何捕获此objectdisposedexception [英] How do I catch this objectdisposedexception
本文介绍了我如何捕获此objectdisposedexception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图通过try-catch捕获异常,但它不起作用
我尝试了什么: < br $> b $ b
I've tried to catch the Exception by try-catch but it isn't works
What I have tried:
try
{
form.Close();
form.Show();
}
catch (ObjectDisposedException)
{
form.Show();
}
推荐答案
无论我是否创建表单实例并尝试:
It works fine for me, regardless of if I create a form instance and try that:
private void Button1_Click(object sender, EventArgs e)
{
FrmMain form = new FrmMain();
try
{
form.Close();
form.Show();
}
catch (ObjectDisposedException)
{
form.Show();
}
}
或者我使用现有表格:
private void Button1_Click(object sender, EventArgs e)
{
try
{
Close();
Show();
}
catch (ObjectDisposedException)
{
Show();
}
}
无论哪种方式,调试器都显示捕获工作正常。
我觉得你很困惑,因为你会得到当您尝试第二次显示表单时,catch处理程序中的相同异常。试试这个:
Either way, the debugger shows the catch working fine.
I think you are confused, because you will get an identical exception inside the catch handler when you try to Show the form for a second time. Try this:
private void Button1_Click(object sender, EventArgs e)
{
FrmMain form = new FrmMain();
try
{
form.Close();
form.Show();
}
catch (ObjectDisposedException ex)
{
MessageBox.Show(ex.Message);
}
}
它应该显示错误。
这篇关于我如何捕获此objectdisposedexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文