473,385 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Prevent Exceptions from "Bubbling up" to the next try catch block

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 causes the exception
}
catch(Exception ex){
//Exception is caught and logged here
}
finally{
//clean up code
}

//Some code that will not run because the exception above does
not get cleared
}
catch(Exception ex){
//Exception from the nested try/catch block gets bubbled up
to this catch block
}

Is this possible? I can't find any way to clear the exception and
continue processing. There are times when I want to continue
processing even when an exception occurs. For example, if I want to
track individuals viewing a web page for marketing purposes, I could
have code on the page to log there page access and then display the
page. There is no business rule to track ever page access so if an
exception occurs while logging page access I do not want to prevent
the user from viewing the page.

Thanks
Nov 16 '05 #1
3 5478
"Travis" <Tr******@hotmail.com> wrote in message
news:19**************************@posting.google.c om...
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 causes the exception
}
catch(Exception ex){
//Exception is caught and logged here

What code do you actually have in the catch?

}
finally{
//clean up code
}

//Some code that will not run because the exception above does
not get cleared

It's not clear what you mean by the above comment. This code WILL be
executed unless the above execption handler throws an exception.

}
catch(Exception ex){
//Exception from the nested try/catch block gets bubbled up
to this catch block
}

Is this possible? I can't find any way to clear the exception and
continue processing. There are times when I want to continue
You clear the exception by not throwing it again.
processing even when an exception occurs. For example, if I want to
track individuals viewing a web page for marketing purposes, I could
have code on the page to log there page access and then display the
page. There is no business rule to track ever page access so if an
exception occurs while logging page access I do not want to prevent
the user from viewing the page.

Thanks

Nov 16 '05 #2
To be clearer here is a another example:

try{
try{
System.IO.MemoryStream memStream = new System.IO.MemoryStream();

Response.BinaryWrite(memStream.ToArray());

//This line of code will cause the exception
ThreadAbortException
//This behavior is expected
Response.End()
}
catch(Exception ex){
ExceptionManager.Publish(ex);
}

LogPageAccess(Session["UserID"].ToString())
}
catch(Exception ex){
ExceptionManager.Publish(ex);
}

In the nested try/catch I have code I know will cause an exception.
If the exception is caused by the Response.End() method I want to
ignore it and move on to the LogPageAccess method. Running this code
as it is will not run the LogPageAccess method because the exception
thrown by Response.End() method gets bubbled up to the parent
try/catch block thus skipping the LogPageAccess method. My question
is there a way to catch an error and clear it so it does not bubble up
to a parent try/catch block?

I suppose this something like... On Error Resume in vb. OK I said it;
no flaming please this is a valid use of logic like On Error Resume
Next because I do not want to ignore other errors that might occur
just the ThreadAbortException exception.
Nov 16 '05 #3
Travis wrote:
To be clearer here is a another example:

try{
try{
System.IO.MemoryStream memStream = new System.IO.MemoryStream();

Response.BinaryWrite(memStream.ToArray());

//This line of code will cause the exception
ThreadAbortException
//This behavior is expected
Response.End()
}
catch(Exception ex){
ExceptionManager.Publish(ex);
}

LogPageAccess(Session["UserID"].ToString())
}
catch(Exception ex){
ExceptionManager.Publish(ex);
}

In the nested try/catch I have code I know will cause an exception.
If the exception is caused by the Response.End() method I want to
ignore it and move on to the LogPageAccess method. Running this code
as it is will not run the LogPageAccess method because the exception
thrown by Response.End() method gets bubbled up to the parent
try/catch block thus skipping the LogPageAccess method. My question
is there a way to catch an error and clear it so it does not bubble up
to a parent try/catch block?

I suppose this something like... On Error Resume in vb. OK I said it;
no flaming please this is a valid use of logic like On Error Resume
Next because I do not want to ignore other errors that might occur
just the ThreadAbortException exception.


Normally the code you have would behave the way you want; however,
ThreadAbortException is a special case. See the docs for
ThreadAbortException - it can be caught, but it is rethrown at the end
of the catch block.

According to KB article 312629, you might be able to replace your call
to Response.End() with a call to
HttpContext.Current.ApplicationInstance.CompleteRe quest() to work around
this problem.

--
mikeb
Nov 16 '05 #4

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

Similar topics

57
by: John Howard | last post by:
I've sent several messages over the last year asking about python - Who teaches python? Is python losing steam? etc. I have noticed, eg, the declinng number of books at my local borders. The last...
3
by: Shiperton Henethe | last post by:
Hi Can anyone tell me how to open up a new normal, full-sized browser window from a small popup window? I'm using something like this. <script language="javascript"> <!--
0
by: djefanten | last post by:
Hi, I have a windows form with a combobox on it (it has the DropDown as DropDownStyle, which means you can type free text in it OR you can select from a list). At the moment that combobox...
18
by: Leslaw Bieniasz | last post by:
Cracow, 28.10.2004 Hello, I have a program that intensively allocates and deletes lots of relatively small objects, using "new" operator. The objects themselves are composed of smaller...
3
by: Robert Oschler | last post by:
I have an IFrame on my web pages that displays useful tips. I have a button next to it that hides and shows it by triggering a Javascript call. I know how to make the iframe invisible by changing...
2
by: JohnR | last post by:
Hi all. In my program I try to handle all obvious potential errors with structured error handling (try-catch) block. What I would like to do is have an 'overall' error handler that would...
1
by: kang jia | last post by:
hi now i would like user to delete what they don;t want to buy after they already put it in shopping cart. i would like to have a pop up box to alert users that they are going to delete what they...
2
by: beydtha | last post by:
Hi there all some times ,, a message is shown at the time of visiting web sites and I could n ot see some flash or animation in the web sites.. So I want to keep my web sites free from popup...
3
by: mistersulu | last post by:
Hi all: I'm using a wx.ListView object with a multi-threaded wxPython app. The list is dynamically generated and accessed across two or more threads. In spite of the fact that I have checks to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.