Try / catch do not catch

In the following code, I purposefully apply "@fooData" to "@ foo111Data" to see if the try statement captures my exception. See below code. But the try / catch statement did not catch and did not display an exception in the MessageBox, and VS2010 just broke and highlighted a line of incorrect code.

try
{
    conn.Open();
    cmd.Parameters.AddWithValue("@foo111Data", dataStrTb1.Text);
    cmd.ExecuteNonQuery();
}
catch (SqlCeException ex)
{
    MessageBox.Show(ex.ToString());
}
finally
{
    conn.Close();
}
+3
source share
5 answers

Did you choose an exception from another type? I suggest you change catchso that he simply catches the general one Exceptionand sees if he chooses a different type.

Put the breakpoint in the statement catchon the line MessageBox.Show, and then you can check Exception.

+8
source

SqlException

catch (SqlException ex)
{
    MessageBox.Show(ex.ToString());
}
+7

SqlCeException , .

In fact, I would use a registrar, not just the mailbox that you are currently doing.

+3
source

Try

try 
{     
conn.Open();     
cmd.Parameters.AddWithValue("@foo111Data", `dataStrTb1.Text);     
cmd.ExecuteNonQuery(); 
} 
catch (SqlException ex) 
{     
MessageBox.Show(ex.ToString()); 
}
catch (Exception ex) 
{     
MessageBox.Show(ex.ToString()); 
} 

finally 
{     
conn.Close(); 
}` 
+3
source

Visual Studio has a function that allows you to stop if an exception is thrown, even if it is caught.

Just click on the “Debug” menu item, then “Exceptions”, then in the “thrown” column and discard certain exceptions that you do not want the debugger to automatically interrupt.

+2
source

All Articles