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

jaredk picture jaredk · Sep 17, 2012 · Viewed 10.6k times · Source

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?

Answer

Carlos Grappa picture Carlos Grappa · Sep 17, 2012

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.