C # try to catch the confusion

I am very new to C #.

When I try to catch something like this:

try { connection.Open(); command.ExecuteNonQuery(); } catch(SqlException ex) { MessageBox.Show("there was an issue!"); } 

How to find out if there was a problem with Open or ExecuteNonQuery ?
What if I named a group of other non-SQL things in Try ?
How do I know what failed?
What does SqlException mean over regular Exception ?
How can a SqlException handle non-SQL errors if I had this code in a Try block?

+6
source share
6 answers

You can add some catches after try block.

 try { connection.Open(); command.ExecuteNonQuery(); } catch(SqlException ex) { MessageBox.Show("there was an issue!"); } catch(Exception ex) { MessageBox.Show("there was another issue!"); } 

An important rule here is to catch the most specific exception above and the more general ones below.

What if I called a bunch of other non-SQL files in TRY? How can I find out what failed?

This will be based on the exception that is thrown by the operation. As I said, first catch the most specific exceptions and get a general idea when you go down. I suggest looking at the MSDN documentation for methods that you think might throw an exception. This documentation describes which exceptions are thrown by methods in which circumstances.

What does SQLEXCEPTION mean over a regular EXCEPTION?

SQLException is an extension of the normal Exception class that is raised only in certain circumstances.

SqlException

How can SQLEXCEPTION handle non-SQL errors if I had this code in a TRY block?

So, in order to answer your question, the blocks you configured would not catch any exception that was not SQLException or extended SQLException .

+7
source

The quick answer is that you can have many catch () parts that catch specific exceptions.

 try{ } catch (SqlException sqlEx) { //deal with sql error } catch (NullArgumentException) { //Deal with null argument }//etc finally { //do cleanup } 

what you really want to do is put things in an attempt to focus on the specific exception that might happen. You also want to have try-catch blocks around the border code (where you don't have control over what happens) and handle your own errors elegantly.

+3
source

You can see in which part of the code your exception occurred catching Exception of SQL

 try { connection.Open(); command.ExecuteNonQuery(); } catch(SqlException ex) // This will catch all SQL exceptions { MessageBox.Show("Execute exception issue: "+ex.Message); } catch(InvalidOperationException ex) // This will catch SqlConnection Exception { MessageBox.Show("Connection Exception issue: "+ex.Message); } catch(Exception ex) // This will catch every Exception { MessageBox.Show("Exception Message: "+ex.Message); //Will catch all Exception and write the message of the Exception but I do not recommend this to use. } finally // don't forget to close your connection when exception occurs. { connection.Close(); } 

InvalidOperationException:

According to MSDN: It is not possible to open a connection without specifying a data source or server. or The connection is already open.

I do not recommend writing entire Exception message in the user interface, as it is more vulnerable to hacking an SQL database

SqlException according to MSDN :

This class is created whenever the .NET Framework Data Provider for SQL Server detects an error generated from the server. (Client-side errors are thrown as standard exceptions for the regular language.) A SqlException always contains at least one SqlError instance.

+2
source

You can use multiple catch blocks for one try block to handle various possible exceptions.
If you use an exception and a print message for this exception, your application will not break, but it will display a message about the exception that occurred. In fact, every catch block defines a different exception, and you can write a specific catch block for an exception only when you are sure that a specific type of exception will occur. otherwise, declare a single catch block, such as catch(Exception ex) , that will be detected for any exception and track errors using the ex.toString() message. You can also use a breakpoint in a try block to get the specific line that caused the exception.

 try { connection.Open(); command.ExecuteNonQuery(); } catch (NullArgumentException ex) { //Deal with null argument ex.toString(); } catch (NumberFormatException ex) { //Deal with null argument ex.toString(); } catch(SqlException ex) { MessageBox.Show("there was an issue!"); } catch(Exception ex) { MessageBox.Show(""+ex.toString()); } 

thanks

0
source

To further define the problem, you can get this syntax:

 try { connection.Open(); command.ExecuteNonQuery(); } catch(Exception ex) { MessageBox.Show("Error:" + ex.Message); // This will display all the error in your statement. } 
0
source

Try and Catch

Actually use in C # Application connect with database

Try
{
string conString = ConfigurationManager.ConnectionStrings ["ldr"]. ConnectionString;

using (SqlConnection con = new SqlConnection (conString))
{
using (SqlCommand cmd = new SqlCommand ("StuProc", con))
{
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue ("@First", firstname.Text);

cmd.Parameters.AddWithValue ("@Last", lastname.Text);

cmd.Parameters.AddWithValue ("@Phone", phonebox.Text);

cmd.Parameters.AddWithValue ("@Email", emailbox.Text);

cmd.Parameters.AddWithValue ("@ is # Intrest", csharExperties.Checked);

cmd.Parameters.AddWithValue ("@isJavaIntrest", javaExperties.Checked);

cmd.Parameters.AddWithValue ("@isVBIntrest", VBExperties.Checked); con.Open ();
cmd.ExecuteNonQuery (); Result DialogResult = MessageBox.Show ("Your data is stored", "Successfully", MessageBoxButtons.OK, MessageBoxIcon.Information);

switch (result)
{
case DialogResult.OK:
ClearScreen ();
break;
}
}
}
}
catch (Exception ex)
{MessageBox.Show ("Problem:", ex.Message);

}
}

If there is any error in your connection, such as configuration, procedure, parameter and Initialization, then it will warn you of your error in this area

-3
source

Source: https://habr.com/ru/post/958048/


All Articles