Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# try catch confusion

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?

like image 485
Nathan Avatar asked Nov 14 '13 05:11

Nathan


People also ask

What C is used for?

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 ...

What is the full name of C?

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 in C language?

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.

Is C language easy?

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.


1 Answers

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.

like image 127
David Pilkington Avatar answered Sep 20 '22 12:09

David Pilkington