nerdexam
Microsoft

98-372 · Question #17

You are creating an application using .NET Framework 4.0. You write the following code segment in the application: try { Console.WriteLine("Connecting to server"); throw new SmtpException("Server…

The correct answer is E. Connecting to server. The output is as follows: The output is as follows: Connecting to server SMTP Error: Server busy Connection closed In the try block, the first Console.WriteLine command runs. The Throw command stops execution and processing resumes in a catch block. Because the first catch…

Understanding Error Handling

Question

You are creating an application using .NET Framework 4.0. You write the following code segment in the application:

try { Console.WriteLine("Connecting to server"); throw new SmtpException("Server busy"); Console.WriteLine("Connected to server"); } catch(SmtpException args) { Console.WriteLine("SMTP Error: " + args.Message); } catch(Exception args) { Console.WriteLine("Error: " + args.Message); } finally { Console.WriteLine("Connection closed"); What is the output of the above code?

Options

  • AConnecting to server
  • BConnecting to server
  • CConnecting to server
  • DConnecting to server
  • EConnecting to server

How the community answered

(35 responses)
  • A
    14% (5)
  • B
    6% (2)
  • C
    6% (2)
  • D
    3% (1)
  • E
    71% (25)

Explanation

The output is as follows: The output is as follows: Connecting to server SMTP Error: Server busy Connection closed In the try block, the first Console.WriteLine command runs. The Throw command stops execution and processing resumes in a catch block. Because the first catch block matches the exception type thrown, processing resumes in the first catch block. After the catch block has finished running, processing continues inside the finally block. The try...catch block is used to handle runtime errors. In an event procedure, the try statement is placed just before the statements that might cause an error. The catch statement is placed just before the list of statements that are to be run if a runtime error occurs. For example: //Statements that might cause a runtime error. //Statements to be run if a runtime error occurs. A block of code that appears just after the finally statement is called a finally block. The statements in a finally block are executed immediately after execution of the try/catch block. The finally block is optional. However, each try statement must have at least one catch block or a finally block. When a finally block is defined in a source code, it is guaranteed to execute, regardless of whether or not an exception is thrown.

Topics

#try-catch-finally#exception handling#SmtpException#code output

Community Discussion

No community discussion yet for this question.

Full 98-372 Practice