This article describes how to handle SystemEvents.Exception event, using the Global events handler classes in Kentico CMS 6.0.
Global events allow you to execute custom actions when some CMS event occurs. For example, if a document is created in the CMS system, you can handle the corresponding event, get information about the new document, and send its content by e-mail or use a third-party component to generate a PDF version of the document. Global events represent an easier way to handle these events when compared to the previous approach, based on using Custom handlers. However, this approach is still supported due to backward compatibility reasons, but the recommended way is to use the
global event handlers instead.
There is just one sample code regarding this topic in our Developer’s guide (CustomDocumentEvents), so let‘s deal with another scenario – handling
exceptions.
Following class and its respective events can be used for this purpose:
-
SystemEvents - contains general system events, replaces the old CustomExceptionHandler
Exception - fired when unhandled exception occurs
To handle this, you can create new class anywhere in your
App_Code folder, and use following sample code:
using CMS.TreeEngine;
using CMS.SettingsProvider;
[SystemEvents]
public partial class CMSModuleLoader
{
/// <summary>
/// Attribute class that ensures the loading of custom handlers
/// </summary>
private class SystemEventsAttribute : CMSLoaderAttribute
{
/// <summary>
/// Called automatically when the application starts
/// </summary>
public override void Init()
{
SystemEvents.Exception.Execute += new System.EventHandler<SystemEventArgs>(Exception_Execute);
}
private void Exception_Execute(object sender, SystemEventArgs e)
{
try
{
// Add custom actions here
throw new System.ApplicationException();
}
catch (System.ApplicationException ex)
{
string sError = ex.Message;
}
}
}
}
-rm-