473,466 Members | 1,388 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 5856
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= " " ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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 ...

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.