Kentico CMS 6.0 Developer's Guide

Exception handler (CustomExceptionHandler class)

Exception handler (CustomExceptionHandler class)

Previous topic Next topic Mail us feedback on this topic!  

Exception handler (CustomExceptionHandler class)

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

This class processes all exceptions that occur in the web application. You can use it to handle all exceptions and run custom actions, such as sending e-mail to administrator or logging the exception to your helpdesk or monitoring system.

 

The class has only one method: OnException(Exception e)

 

Example

 

The following example shows how to handle the OnException event and send the password to the administrator whenever an error occurs:

 

1. Open the CustomExceptionHandler class and put the following code at the beginning of the file. It adds the reference to the namespaces we will use to handle the event:
 

[C#]

 

using CMS.EmailEngine;

 

2. Put the following code inside the OnException method.

 

[C#]

 

// we will use the CMS.EmailProvider to send e-mails

EmailMessage email = new EmailMessage();

email.From = "admin@domain.com";

email.Recipients = "admin@domain.com";

email.Subject = "Exception";

email.Body = e.Message + "<br />" + e.StackTrace + "<br />" + e.Source;

EmailSender.SendEmail(email);              

 

3. Set the From and Recipients e-mail addresses to your e-mail address.
 

4. Compile and run the project. Now, when some exception occurs or is raised by your code, the e-mail with exception details will be sent.