Is there a way to write an if statement inside a catch block?

Viewed 85

What i want is, If no exception raised, the buttons 6 and 8 become visible to user. The finally block runs regardless of exception, so i wouldnt put it there

        if (worksheet == null)
            return;
        try
        {
            Range allo = worksheet.Rows.Cells[72, 2];
            allo.Value = AcceptableNoise.SelectedItem.ToString();
        }
        catch (Exception ee)
        {
            if (ee.Data != null)
            {
                _ = MessageBox.Show("One or more Options not selected " +
               "\n Eg: Confirm that Actuator Type is selected"/*ee.ToString()*/, "Selection Error", 
                MessageBoxButtons.OK, MessageBoxIcon.Hand);

            }
            else
            {
                button8.Visible = true;
                button6.Visible = true;
            }

        }
        
        
        finally
        {

            excel2.DisplayAlerts = false;
            excel2.ActiveWorkbook.Save();
            excel2.Application.Quit();
            excel2.Quit();


            /// = MessageBox.Show("You are done");
        }
3 Answers

Here is some code sample that should work. If it is all correct, buttons will appear. Unless they will hide or remain hidden.

try
{
    Range allo = worksheet.Rows.Cells[72, 2];
    allo.Value = AcceptableNoise.SelectedItem.ToString();
    button8.Visible = true;
    button6.Visible = true;
}
catch (Exception ee)
{
    MessageBox.Show("One or more Options not selected " +
        "\n Eg: Confirm that Actuator Type is selected"/*ee.ToString()*/, 
        "Selection Error", 
        MessageBoxButtons.OK, MessageBoxIcon.Hand);

    button8.Visible = false;
    button6.Visible = false;
}               
finally
{
    excel2.DisplayAlerts = false;
    excel2.ActiveWorkbook.Save();
    excel2.Application.Quit();
    excel2.Quit();

    MessageBox.Show("You are done");
}

But one condition is very important - code before button8.Visible = true has to throw an exception when needed. If not, buttons will appear anyway.

What I want is, If no exception raised, the buttons 6 and 8 become visible to user.

And what do you want if an exception is raised?

try
    {
        Range allo = worksheet.Rows.Cells[72, 2];
        allo.Value = AcceptableNoise.SelectedItem.ToString();

        // If here, then no exception raised. Do what you want if no exception raised:
        button6.Visible = true;
        button8.Visible = true;
    }
    catch (Exception exc)
    {
        // if here, an exception has been raised
        // do what you want, for example hide the buttons
        button4.Visible = false;
        button6.Visible = false;
    }

Create a Boolean at the top such as “bool exceptionFound = false” If an exception, then set the value to “true”. Then after the “final”, act on the Boolean value.

bool exceptionFound = false;
try
{
}
catch (Exception ee)
{
    exceptionFound = true;
}
finally
{
}
if (!exceptionFound) {
    // Set  buttons 6 and 8 visible
}
Related