Professional DotNetNuke 4: Open Source Web Application Framework for ASP.NET 2.0 (Programmer to Programmer)

Exception Handling

The exception handling API in DotNetNuke provides a framework for handling exceptions uniformly and elegantly. Exception handling primarily uses four methods, most of which have several overloaded methods. Through these methods, developers can gracefully handle exceptions, log the exception trace and context, and display a user-friendly message to the end user.

The Exception Handling API

The exception handling API lives under the DotNetNuke.Services.Exceptions namespace. Table 8-2 lists the classes that comprise the Exception Handling API.

Table 8-2: Exception Handling Classes

Class

Description

BasePortalException

Inherits from System.Exception and contains many other properties specific to the portal application.

ErrorContainer

Generates formatting for the error message that will be displayed in the web browser.

ExceptionInfo

Stores information from the stack trace.

Exceptions

Contains most of the methods that are used in custom modules. It contains the methods necessary to process each type of portal exception.

ModuleLoadException

An exception type for exceptions thrown within portal modules. It inherits from BasePortalException.

PageLoadException

An exception type for exceptions thrown within pages.

SchedulerException

An exception type for exceptions thrown within the Scheduling Provider. It also inherits from BasePortalException.

The Exceptions Class

Although there are many classes in the exception handling namespace, the primary class that module developers deal with regularly is the Exceptions class. This class contains all of the methods necessary to gracefully handle exceptions in DotNetNuke. The most widely used method for exception handling is DotNetNuke.Services.Exceptions.ProcessModuleLoadException().

ProcessModuleLoadException Method

The ProcessModuleLoadException method serves two primary functions: to log the exceptions that are thrown from within a module to the Logging Provider, and to display a friendly error message in place of the module that threw the exception. The friendly error message is displayed only if the host option Use Custom Error Messages is enabled on the Host Settings page (see Chapter 5).

ProcessModuleLoadException has seven overloaded methods:

ProcessPageLoadException Method

Similar to the ProcessModuleLoadException method, the ProcessPageLoadException method serves two primary functions: to log the exceptions thrown from outside of a module to the Logging Provider, and to display a friendly error message on the page. The friendly error message will only be displayed if the host option Use Custom Error Messages is enabled on the Host Settings page (see Chapter 5).

ProcessPageLoadException has two overloaded methods:

LogException Method

The LogException method is used for adding exceptions to the log. It does not handle displaying any type of friendly message to the user. Instead, it simply logs the error without notifying the client browser of a problem. LogException has four overloaded methods:

ProcessSchedulerException Method

The ProcessSchedulerException method is used to log exceptions thrown from within a scheduled task. It simply logs the error.

To log an exception thrown from a scheduled task, use the following overloaded method:

Public Sub LogException(ByVal exc As ModuleLoadException)

Parameter

Type

Description

exc

ModuleLoadException

Exception that was thrown.

The exception handling API abstracts developers from the complexity of logging exceptions and presenting error messages gracefully. It provides several powerful methods that handle all of the logic involved in working with the Logging Provider and the presentation layer. The next section covers the various interfaces that module developers can take advantage of to bring more core features to life in their modules.

Категории