Sunday, 16 December 2012

Response.end() throws excetion


Recently working on a functionality in my project .
i came across a very irritating situation . when i run it locally it runs successfully but on server it throws error ThreadAbortException.
started looking in the code nothing is wrong here man what i am searching in code there is something wrong on server.
After doing a lot of research and analysis found the culprit and it was response.End() .
so i tried to put a try catch block to absorb exception .
It should work right but it did not . 
Again whats wrong man!! sometimes it quite irritating @ workplace. :D
After going through MSDN i found that Response.end() tries to abort the thread on which its executing (again a very interesting concept by Microsoft) 
if it is always going to throw the exception what the hell you gave it to world to use it .:x
now we have response.end() and we have to use it to remove the unwanted characters from the file.
here is a way around use it like :

 1) use return statement

try
{
response.end()
}
catch(ThreadAbortException ex)
{
return;  /// it is the code missing in most of site you will search
}
catch(Exception ex)
{
}

2) it will solve the problem , but should not use

in global.asax add code to ignore threadabortexception
If you have a better way then please comment

No comments:

Post a Comment