Hellcat8 Hellcat8 -4 years ago 97
SQL Question

SqlException not caught in Winforms

I got this code:

using (SqlConnection sourceCnx = new SqlConnection(SOURCE_CONN_STRING))
SqlCommand sysCmd = sourceCnx.CreateCommand();
sysCmd.CommandText = "My query";
catch (Exception)
//Throwing a message box informing that there is an error

I want to display a message when the user is no longer connected to the internet.
But when I debug my program without an internet connection, the program crashes with a SqlException. (The "catch" block does not catch the exception)

I tried to add
catch (SqlException) { // code }
catch (Exception)
but it doesn't work.
I still have an exception instead of a message displayed by the catch block.

I don't know what to do because if I create a method to test the internet connection (
to ping google.com) then return true if it's ok, it will be the same : I got an exception because of no internet connection.

Any idea?

Answer Source

You have set your environment to always break when a CLR exception is thrown. You can leave it like that if you wish and press F5 to carry on execution of your program. Or you can turn this off (it is switched off by default):

Go to Debug menu, select Exceptions and ensure Common Language Runtime Exceptions is not checked.

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download