How should you write the catch block?

DRAG DROP
You are creating a method that saves information to a database.
You have a static class named LogHelper. LogHelper has a method named Log to log the
exception.
You need to use the LogHelper Log method to log the exception raised by the database
server. The solution must ensure that the exception can be caught by the calling method,
while preserving the original stack trace.
How should you write the catch block? (Develop the solution by selecting and ordering the
required code snippets. You may not need all of the code snippets.)

DRAG DROP
You are creating a method that saves information to a database.
You have a static class named LogHelper. LogHelper has a method named Log to log the
exception.
You need to use the LogHelper Log method to log the exception raised by the database
server. The solution must ensure that the exception can be caught by the calling method,
while preserving the original stack trace.
How should you write the catch block? (Develop the solution by selecting and ordering the
required code snippets. You may not need all of the code snippets.)

Answer: See the explanation.

Explanation:
Box 1:

Box 2:

Box 3:
throw;
Box 4:

Note:
Catch the database exception, log it, and then rethrow it.

* SQLException
An exception that provides information on a database access error or other errors.



Leave a Reply 3

Your email address will not be published. Required fields are marked *


PaulC

PaulC

Solution

catch (SqlException ex)
{
LogHelper.Log(ex);
throw;
}

reason: using “throw” preserves the original stack trace.

Mahbub

Mahbub

PaulC is correct