PC Review


Reply
Thread Tools Rate Thread

Bubbling Exceptions

 
 
Chuck Bowling
Guest
Posts: n/a
 
      18th Dec 2006
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
==========================================


 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      18th Dec 2006
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
> ==========================================
>



 
Reply With Quote
 
Chuck Bowling
Guest
Posts: n/a
 
      18th Dec 2006
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 [.NET/C# MVP]" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> 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
>> ==========================================
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bubbling up exceptions pamelafluente@libero.it Microsoft ASP .NET 11 29th Sep 2006 06:01 PM
Bubbling exceptions and destructors =?Utf-8?B?U2hhd24gTWVsdG9u?= Microsoft Dot NET 1 19th Mar 2005 09:34 AM
Bubbling Exceptions thrown in a Remoted Object Z D Microsoft C# .NET 3 12th Jan 2005 11:03 PM
Bubbling Exceptions thrown in a Remoted Object Z D Microsoft VB .NET 3 12th Jan 2005 11:03 PM
Prevent Exceptions from "Bubbling up" to the next try catch block Travis Microsoft C# .NET 3 6th May 2004 07:46 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:39 PM.