Displaying User-Friendly Messages

07/25/2009

You may want to replace the message in the original exception with a more appropriate, user-friendly message. To do this, you must replace the original exception with another exception that has a more appropriate message associated with it. For example, exceptions that occur in the data access layer of an application can be replaced with an exception of type System.ApplicationException. This uses the message, "The application is unable to process your request at this time." This message is then displayed to the user.

Typical Goals
You want to display a user-friendly message when an exception occurs. Your application has code in its catch blocks similar to the following.

Note:
The code does not include implementations of the FormatException and Logger.Log methods. These methods represent typical ways to create and log a formatted exception message.

C# Copy Code 
catch(SomeException e)
{
    string formattedInfo = FormatException(e);
    Logger.Log(formattedInfo);
    throw e;
}

Visual Basic Copy Code 
Catch e As SomeException
  Dim formattedInfo As String = FormatException(e)
  Logger.Log(formattedInfo)
  Throw e
End Try

Solution
Use either a wrap handler or replace handler to create a new exception with the appropriate message.

QuickStart
For an extended example of how to use the Exception Handling Application Block to display a user-friendly message, see the QuickStart walkthrough, Walkthrough: Notifying the User.

Displaying User-Friendly Messages
The following procedure describes how to use the Exception Handling Block to display user-friendly messages.

To display user-friendly messages

Use the configuration tools to create an exception handling policy with the relevant exception types for your application. For more information, see Entering Configuration Information.
Configure the exception type. Specify the PostHandlingAction as ThrowNewException. The ThrowNewException action indicates that the application block will throw the exception that is returned from the last exception handler in the chain.
Add a new wrap exception handler for the specified exception types.
Configure the wrap exception handler with the new exception type and friendly message.
Modify your application code to execute the new policy when an exception occurs.C# Copy Code 
try
{
  // Run code.
}
catch(Exception ex)
{
  bool rethrow = ExceptionPolicy.HandleException(ex, "Wrap Policy");
  if (rethrow)
    throw;
}

Visual Basic Copy Code 
Try
  ' Run code.
Catch ex As Exception
  Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Wrap Policy")
  If (rethrow) Then
' throw original exception
    Throw
  End If
End Try

Usage Notes
If you use the Unity Integration approach to create instances of objects from the Exception Handling Application Block, you must use the non-static façade named ExceptionManager instead of the ExceptionPolicy class static façade.

Posted in: C# and .NET| Tags: Display Enterprise Library ExceptionPolicy User-Friendly Message handler Unity Integration static Facade

Displaying available command types

05/27/2009

The Get-Command command does not list every command that is available in Windows PowerShell. Instead, the Get-Command command lists only the cmdlets in the current shell. Windows PowerShell actually supports several other types of commands. Aliases, functions, and scripts are also Windows PowerShell commands, although they are not discussed in detail in the Windows PowerShell Primer. External files that are executables, or have a registered file type handler, are also classified as commands.

You can return a listing of all items that can be invoked by entering the following command:

PS> Get-Command *

Because this list includes external files in your search path, it may contain thousands of items. It is more useful to look at a reduced set of commands. To find native commands of other types, you can use the CommandType parameter of the Get-Command cmdlet. Although we have not talked about these other command types yet, you can still display them if you know the name of the CommandType for a class of commands.

Note:

Although we have not discussed it yet, the asterisk (*) is used for wildcard matching in Windows PowerShell command arguments. The * means "match one or more of any characters". You can type Get-Command a* to find all commands that begin with the letter "a". Unlike wildcard matching in Cmd.exe, Windows PowerShell's wildcard will also match a period.

To display the special command category aliases (these are nicknames used as alternatives to standard command names), enter the following command:

PS> Get-Command -CommandType Alias

To display all Windows PowerShell functions, enter the following command:

PS> Get-Command -CommandType Function

To display external scripts in Windows PowerShell's search path, enter the following command:

PS> Get-Command -CommandType ExternalScript

Posted in: Software| Tags: Windows PowerShell Command Display Get external displaying primer detail aliases shell

Hot Posts

Latest posts

Tags

Others

Sponsors

asp.net interview questions