Try/Catch not catching System.Threading.ThreadAbortException in ReportDocument.ExportToHttpResponse
Asked Answered
G

1

6

I am trying to export a Crystal ReportDocument using ExportToHttpResponse like so:

report.ExportToHttpResponse(exportOptions, HttpContext.Current.Response, true, "test");

When I first tried to run this, I received a System.Threading.ThreadAbortException. After reading about how this is a known error with ExportToHttpResponse in this question, I tried implementing the suggested workaround of wrapping the statement in a try/catch block like so:

try
{
    report.ExportToHttpResponse(expOptions, HttpContext.Current.Response, true, "test");
}
catch (System.Threading.ThreadAbortException e)
{
}

As I understand it, this should catch and ignore the error, and proceed. However, I am still getting the System.Threading.ThreadAbortException on the closing bracket of the catch statement. My question is why is the exception still being received even though I am apparently catching it, and how could I go about fixing it so that the exception is ignored?

Grith answered 17/9, 2012 at 19:34 Comment(2)
"ThreadAbortException is a special exception that can be caught, but it will automatically be raised again at the end of the catch block.". See the remarks section: msdn.microsoft.com/en-us/library/…Asparagine
Where are you seeing these errors? Are you logging them in a global error handler? If this is in a global error handler, such as Applicaiton_Error in global.asax, you simply need to filter those out and not do anything with them.Horseshoes
S
8

You can catch the ThreadAbortException and call the Thread.REsetAbort method, to cancel the bubbling of the exception. However, keep in mind that response.end is a bad idea. Whenever you can try to call HttpApplication.CompleteRequest(), and read this SO question which proved really useful to me in this regard.

Stenosis answered 17/9, 2012 at 19:44 Comment(1)
Thread.ResetAbort did fix my problem, thanks. I am aware that Response.End is a bad idea, but I am not the one calling it in this scenario. Thanks for the additional information as well.Grith

© 2022 - 2024 — McMap. All rights reserved.