Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Try-Catch-Finally on Return [duplicate]

I have the following code:

public DataTable GetAllActiveUsers()
{
            DataTable dataTable = new DataTable();

            try
            {
                connection.Open();

                SqlCommand getAllActiveUsersCommand = new SqlCommand(getAllUsers, connection);

                SqlDataAdapter dataAdapter = new SqlDataAdapter(getAllActiveUsersCommand);
                dataAdapter.Fill(dataTable);

                return dataTable;
            }
            catch(Exception e)
            {
                Console.WriteLine(e);

                return null;
            }
            finally
            {
                connection.Close();
            }
        }

Which basically get's the active users I have on my database. But can someone explain to me whether the Finally Block will be executed if it successfully runs the try block and returns the DataTable??

Thanks

like image 920
Danny Avatar asked Dec 22 '15 10:12

Danny


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.


3 Answers

Yes.

As stated here: MSDN

Typically, the statements of a finally block run when control leaves a try statement. The transfer of control can occur as a result of normal execution, of execution of a break, continue, goto, or return statement, or of propagation of an exception out of the try statement.

But finally block is not always executed. You can read Alex Papadimoulis's anecdote here

like image 99
gyosifov Avatar answered Sep 25 '22 07:09

gyosifov


Yes, it does.
The finally block will be executed whether there is a return statement or an exception thrown in the try {} catch() block.

like image 45
croxy Avatar answered Sep 24 '22 07:09

croxy


finally block is always executed.

you should Dispose in finally block. Because, dispose also closes the connection and disposes unmanaged memory resources.

finally
{
    connection.Dispose();
}
like image 34
mehmet mecek Avatar answered Sep 24 '22 07:09

mehmet mecek