To display sharepoint friend error page, you need to change the web.config file and
make sure:

<customErrors mode=”On” />
<SafeMode MaxControls=”200″ CallStack=”false” …>

And if you get an error in your custom code that you want to handle using the “out of the box” SharePoint mechanisms, use the following lines of code…

Make sure you only call the ‘TransferToErrorPage’ method if you are handling errors in the a user interface (web part, page, etc…). Don’t call that method in a workflow, event handler, etc…

//This will write to SharePoint’s log files.

Microsoft.Office.Server.Diagnostics.PortalLog.LogString(“Your Error Message”);

//This will redirect you to SharePoint’s error page.

Microsoft.SharePoint.Utilities.SPUtility.TransferToErrorPage(“Message to display on the page”);