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

T

Travis

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
 
J

John Vottero

Travis said:
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.
 
T

Travis

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

mikeb

Travis said:
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.CompleteRequest() to work around
this problem.
 

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