Exception handling in your console application

The ConsoleFx frameworks throw two kinds of exceptions:
  • System.ArgumentException and it's derived exception classes (ArgumentNullException and ArgumentOutOfRangeException), which are thrown for all issues with the inputs to ConsoleFx's public APIs.
  • Exceptions deriving from the ConsoleFx.ConsoleFxException class, typically ParserException and ConsoleProgramException, which indicate errors in the core processing logic of ConsoleFx.
ConsoleFxExceptionClasses.png

Built-in exception handling

All types of ConsoleFx console applications (simple, complex, interactive and declarative) provide basic exception handling for the above types of exceptions. This built-in error handling categorizes exceptions into two types:

Internal exceptions

These are technical errors with the ConsoleFx framework code or with the console application's code, and normally require a change to the code to fix. Since they are not user-caused exceptions, their details are not displayed at run-time; instead a generic message is displayed indicating that an internal error has occurred. (In the next version of ConsoleFx, the console application developer will be able to configure logging of the internal exception details to a location of her choice.)

ConsoleFx considers the following exceptions to be internal exceptions:
  • System.ArgumentException and derived exceptions
  • Any ConsoleFx.ConsoleFxException instance where the error code is a negative value.

Applications exceptions

These are normal exceptions that are thrown from various areas in the ConsoleFx framework, such as the command-line parser, the validators, etc. They normally occur due to user input in the console application, and so are displayed to the end-user. As part of its built-in exception handling,
ConsoleFx considers any ConsoleFx.ConsoleFxException exceptions where the error code is a positive value to be an application exception.

Custom exception handling

To handle custom exception types, the ConsoleProgram classes provide a method called SetErrorHandler to set handlers for custom exceptions:

public void SetErrorHandler<TException>(ErrorHandler handler)
    where TException : Exception

ErrorHandler is a delegate that accepts the exception type and allows you to specify how to handle exceptions of that type:

public delegate void ErrorHandler(Exception exception);

Last edited May 3, 2012 at 10:08 AM by jeevanjj, version 7

Comments

No comments yet.