programatically redirect to custom error page

To set a custom error page

const string CustomErrorPage = "/_layouts/CustomPageMappings/Error.aspx"; 

webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.Error, CustomErrorPage);

ref:
http://todd-carter.com/post/2010/04/07/An-Probable-Error-Has-Occurred.aspx

To call the error page ( custom or defaulting )
You can use the SPUtility.TransferToErrorPage method in any Web Part or custom code

try
{
//Code that force cause an exception
}
catch (Exception ex)
{
SPUtility.TransferToErrorPage(ex.Message);
}

ref:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.utilities.sputility.transfertoerrorpage.aspx

Check it out:SharePoint 2010