Use the LinePrinterException Class

The LinePrinter class will throw only a LinePrinterException on an error. The LinePrinterException class is derived from System.Exception and inherits all functionality from that class.  

Three fields in particular are very important in determining what caused the error: ErrorType, ErrorCode, and PortName. These fields are interpreted as follows:

ErrorType = CONFIG_ERROR

ErrorCode Description
ERROR_INVALIDXML XML Printer Configuration file is flawed or missing.
ERROR_REGISTERDEVICE LinePrinter could not register a device driver needed for the selected PrinterPort.
ERROR_NOT_SUPPORTED XML Printer Configuration file is specifying that an unsupported port has been selected.

ErrorType = PRINTER_ERROR

PortName ErrorCode and Description
NPCP One of the NPCP protocol error codes.
IRDA

If ErrorCode is ERROR_NOIRDADEVICE: No IRDA device in range.

If ErrorCode is COR_E_IO: Error was caused by a System.IO.IOException that will be contained in the embedded exception.

SERIAL The value returned by GetLastError.
WIFI Standard HRESULT that may be looked up via the Visual Studio Error Lookup tool.

ErrorType = SYSTEM_ERROR

An unexpected system error has occurred.

ErrorCode Property

This property identifies the printer error that occurred.  This error code needs to be interpreted in the context of the ErrorType and PortName properties.

public int ErrorCode
Read Only:

Example:

Message = string.Format("NPCP PRINTER ERROR\n\r\n\rError # {0} - {1}" ,lpe.ErrorCode, NPCPErrorStr(lpe.ErrorCode));

ErrorType Property

This property returns an ErrorTypes enum that indicates the type of error thrown.  The definition of ErrorTypes is:

public enum ErrorTypes

It contains the elements PRINTER_ERROR, CONFIG_ERROR, SYSTEM_ERROR.

public ErrorTypes ErrorType
Read Only:

Example:

if (lpe.ErrorType == LinePrinterException.ErrorTypes.CONFIG_ERROR)

InnerException Property

Inherited from System.Exception. If the LinePrinterException was caused by a different exception, such as a System.Exception, SerialPortException, or a NPCPPrintException, then that exception will be passed here.

public Exception InnerException
Read only.

Example:

if (lpe.InnerException is System.Exception)

Message Property

Derived from System.Exception. This is a human readable message describing the cause of the exception. This is designed for code debugging only and is not meant to be used as an error description displayed to the end user. It will not be internationalized.

public virtual string Message
Read only.

Example:

string str = lpe.Message;

MethodCategory Property

This is an indication of what was happening when the error occurred.

public LinePrinter.MethodCategoryCode MethodCategory;
Read Only:

The definition of MethodCategoryCode is:

public enum MethodCategoryCode : ushort

It contains the elements Constructor, Open, Close, Write, and Administration.

Example:

If (lpe.Methodcategory == LinePrinter.MethodCategoryCode.Open)

MethodId Property

This property tells what method was in operation when the exception occurred.

public LinePrinter.MethodIdentification MethodId;
Read Only:

The definition of MethodIdentification is:

public enum MethodIdentification : uint

It contains the elements ActualFormFeed, BoldOff, BoldOn, Cancel, Close, Compress, DoubleWide, FlushBuffer, FontSelect, FormFeed, ItalicOff, ItalicOn, LinePrinter, LineWillFit, NewLine, NoOrphan, Normal, Open, ResetFont, StrikeOutOff, StrikeOutOn, UnderlineOff, UnderlineOn, and Write.

Example:

If (lpe.MethodId == LinePrinter.MethodIdentification.Write)

MethodName Property

This property returns a string that is the name of the method that was in operation when the exception was thrown.

public string MethodName;
Read Only:

Example:

string str = "The name of the function is :" + lpe.MethodName;

Namespace Property

This property will always return the string "Honeywell.Mobility.Print".

public string Namespace;
Read Only:

Example:

String str = "The namespace name is" + lpe.Namespace;

PortName Property

This property returns a string that is the name of the port that the printer is connected to.

public string PortName
Read Only:

TypeName Property

This string property will always return "LinePrinter".

public string TypeName;
Read only.

Example:

string ClassName = lp.TypeName;

Exception Handler Example

A simple example of an exception handler for a LinePrinter based report could be:

public void ShowError(Exception e)
{
    string Message = "UNKNOWN ERROR WHILE PRINTING";
    if (e is LinePrinterException)
    {
        LinePrinterException lpe = (LinePrinterException)e;
        if (lpe.ErrorType == LinePrinterException.ErrorTypes.CONFIG_ERROR)
        {
            Message = "PRINTER CONFIGURATION ERROR\r\n\r\nPossibly invalid configuration file!";
        }
        else if (lpe.ErrorType == LinePrinterException.ErrorTypes.PRINTER_ERROR)
        {
            Message = string.Format("NPCP PRINTER ERROR\n\r\n\rError # {0} - {1}"
              ,lpe.ErrorCode,NPCPErrorStr(lpe.ErrorCode));
        }
    }
    MessageBox.Show(Message,"REPORT ERROR!",MessageBoxButtons.OK, MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1);
}

There is other cleanup that must be done when an exception is caught, such as releasing all the event handlers and calling the Close() method.

More Information

Use the LinePrinter Class