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
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.
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
Yes, it does.
The finally block will be executed whether there is a return statement or an exception thrown in the try {} catch()
block.
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();
}
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