Bubbling Exceptions

C

Chuck Bowling

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
==========================================
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
C

Chuck Bowling

Thanks Nicholas. That's exactly what I thought. The reason for the confusion
is that it conflicts directly with the answer provided by the study guide.


Nicholas Paldino said:
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 address removed)

Chuck Bowling said:
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
==========================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top