Microsoft Exam Questions

Which code segment should you insert at line 11?

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4.0 to create an application.
You create a Database Access Layer (DAL) that is database-independent. The DAL includes the following code segment.
(Line numbers are included for reference only.)

01 static void ExecuteDbCommand(DbConnection connection)
02 {
03 if (connection != null){
04 using (connection){
05 try{
06 connection.Open();
07 DbCommand command = connection.CreateCommand();
08 command.CommandText = “INSERT INTO Categories (CategoryName) VALUES (‘Low Carb’)”;
09 command.ExecuteNonQuery();
10 }
11 …
12 catch (Exception ex){
13 Trace.WriteLine(“Exception.Message: ” + ex.Message);
14 }
15 }
16 }
17 }

You need to log information about any error that occurs during data access.
You also need to log the data provider that accesses the database. Which code segment should you insert at line 11?

A.
catch (OleDbException ex){
Trace.WriteLine(“ExceptionType: ” + ex.Source);
Trace.WriteLine(“Message: ” + ex.Message);
}

B.
catch (OleDbException ex){
Trace.WriteLine(“ExceptionType: ” + ex.InnerException.Source);
Trace.WriteLine(“Message: ” + ex.InnerException.Message);
}

C.
catch (DbException ex){
Trace.WriteLine(“ExceptionType: ” + ex.Source);
Trace.WriteLine(“Message: ” + ex.Message);
}

D.
catch (DbException ex){
Trace.WriteLine(“ExceptionType: ” + ex.InnerException.Source);
Trace.WriteLine(“Message: ” + ex.InnerException.Message);
}

Explanation:
Exception.InnerException Gets the Exception instance that caused the current exception.
Message Gets a message that describes the current exception.
Exception.Source Gets or sets the name of the application or the object that causes the error.

OleDbException catches the exception that is thrown only when the underlying provider returns a warning or error for an OLE DB data source.
DbException catches the common exception while accessing data base.