I'm very new to C#.
When I try catch something like this:
try
{
connection.Open();
command.ExecuteNonQuery();
}
catch(SqlException ex)
{
MessageBox.Show("there was an issue!");
}
How do I know if the problem happened with the Open
or ExecuteNonQuery
?
What if I called a bunch of other non-SQL
stuff in the Try
?
How would I know which failed?
What does SqlException
mean over regular Exception
?
How would SqlException
handle non-SQL related errors if I had such code in the Try
block?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
You can add multiple catches after the 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!");
}
The important rule here to to catch the most specific exception higher up and the more general ones lower down
What if I called a bunch of other non-SQL stuff in the TRY? How would I know which failed?
This will be based on the exception that is thrown by the operation. Like I say, catch the most specific exceptions first and get more general as you go down. I suggest going to look at the MSDN documentation for methods that you think may thrown an exception. This documentation details what exceptions are thrown by methods in which circumstances.
What does SQLEXCEPTION mean over regular EXCEPTION?
SQLException
is an extension of the normal Exception
class that is only thrown in certain circumstances.
SqlException
How would SQLEXCEPTION handle non-SQL related errors if I had such code in the TRY block?
So to answer your question, the blocks that you had set up would not catch any exception that was not a SQLException
or extended SQLException
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With