Microsoft
98-361 · Question #331
You have written a C# method that opens a database connection by using the SqlConnect object. The method retrieves some information from the database and then closes the connection. You need to make…
The correct answer is D. Inside the finally block. See the full explanation below for the reasoning.
Question
You have written a C# method that opens a database connection by using the SqlConnect object. The method retrieves some information from the database and then closes the connection. You need to make sure that your code fails gracefully when there is a database error. To handle this situation, you wrap the database code in a try-catch-finally block. You use two catch blocks--one to catch the exceptions of type SqlException and the second to catch the exception of type Exception. Which of the following places should be the best choice for closing the SqlConnection object?
Options
- AInside the try block, before the first catch block
- BInside the catch block that catches SqlException objects
- CInside the catch block that catches Exception objects
- DInside the finally block
How the community answered
(42 responses)- A7% (3)
- B17% (7)
- C5% (2)
- D71% (30)
Community Discussion
No community discussion yet for this question.