Click here to Skip to main content
15,905,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've tried to catch the Exception by try-catch but it isn't works

What I have tried:

C#
try
{
    form.Close();
    form.Show();
}
catch (ObjectDisposedException)
{
    form.Show();
}
Posted
Updated 25-Apr-19 2:32am

1 solution

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();
        }
    }
Or I use the existing form:
private void Button1_Click(object sender, EventArgs e)
    {
    try
        {
        Close();
        Show();
        }
    catch (ObjectDisposedException)
        {
        Show();
        }
    }
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);
        }
    }
It should show you the error.
 
Share this answer
 
Comments
Member 14145167 25-Apr-19 10:13am    
OK how am i fix it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900