Chuck,
The answer is A.
If you go with B, then what happens when you throw ex would be that the
call stack for the exception will start from AcceptRequest, and not
Validate, and you would actually have less information. If you just call
Validate and you don't have a try/catch block, then the exception will
bubble up.
If you wanted to do some logging, but still wanted to retain stack
information, you would have a modified version of B:
try
{
this.Validate();
}
catch (Exception ex)
{
// Do your processing of the exception here.
// Throw the original exception.
throw;
}
Notice that it's just "throw". This will preserve the exception and all
stack information.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
-
(E-Mail Removed)
"Chuck Bowling" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> In studying for the 70-316 I ran across the question below. I'm a little
> confused by the 'correct' answer. Why is it necessary to wrap the Validate
> method in a try/catch block? Doesn't the Exception that's thrown in the
> call to Validate propagate up the call stack to the parent form without
> any need to rethrow it?
>
>
> ==========================================
>
> Create a component named Request. This component includes a method named
> AcceptRequest that attempts to process new user requests for services. The
> AcceptRequest component calls a private function named Validate.
>
> You must ensure that any exceptions encountered by Validate are bubbled up
> to the parent form of Request. The parent form will then be responsible
> for handling the exceptions. You want to accomplish this while writing the
> minimum amount of code.
>
> What should you do?
>
> Use the following code segment in AcceptRequest:
>
> A) this.Validate();
>
> B) try {
> this.Validate();
> }
> catch(Exception ex) {
> throw ex;
> }
>
> Answer: B
> ==========================================
>