473,473 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Bubbling Exceptions

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
==========================================
Dec 18 '06 #1
2 5858
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]
- mv*@spam.guard.caspershouse.com

"Chuck Bowling" <ch**********@sbcglobal-NO-SPAM.netwrote in message
news:uV*************@TK2MSFTNGP06.phx.gbl...
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
==========================================

Dec 18 '06 #2
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]" <mv*@spam.guard.caspershouse.comwrote in
message news:uV**************@TK2MSFTNGP04.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Chuck Bowling" <ch**********@sbcglobal-NO-SPAM.netwrote in message
news:uV*************@TK2MSFTNGP06.phx.gbl...
>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
==========================================


Dec 18 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Shawn Melton | last post by:
Does a caught exception still bubble up and will a destructor execute if an exception halts execution?
3
by: Travis | last post by:
I am trying to prevent an exception from bubbling up from one nested Try/Catch Block to its "Parent" Try/Catch Block. Here is some example code: try{ try{ //Non-application killing code that...
3
by: Z D | last post by:
Hello, I have a remoted object which throws an exception if something goes bad. eg: throw new Exception("Something went wrong."); When I test the assembly locally on my machine (without...
4
by: Wee Bubba | last post by:
the following event handler checks to see if any parent control has attached to the event 'SearchItemSelect'. i use it for event bubbling whenever one of my search items has been selected and at...
3
by: Nathan Sokalski | last post by:
I have three LinkButtons in the HeaderTemplate of my DataList (I use them to let the user determine what to sort the list by). I am assuming that the event will be bubbled to the ItemCommand event...
7
by: comzy | last post by:
I have created an event bubbling for my pager control which is used for implementing paging in data grid. Althoug it worked very fine in .NET 1.1 it is throwing the following error after i migrated...
11
by: pamelafluente | last post by:
Hi guys, When bubbling some exception up to some interface handlers - what is most recommandable: Try 'some code Catch ex As Exception Throw
4
by: pamelafluente | last post by:
Hi guys, When bubbling some exception up to some interface handlers - what is most recommandable: Try 'some code Catch ex As Exception
2
by: alhalayqa | last post by:
hi, in MSIE, you can attach onclick event to both anchor and document objects. here is some code : document.onclick = function() {alert('document clicked ...'); } <a href= " " ...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.