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:
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. |
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. |
An unexpected system error has occurred.
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));
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)
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)
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;
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)
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)
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;
This property will always return the string "Honeywell.Mobility.Print".
public string Namespace;
Read Only:
Example:
String str = "The namespace name is" + lpe.Namespace;
This property returns a string that is the name of the port that the printer is connected to.
public string PortName
Read Only:
This string property will always return "LinePrinter".
public string TypeName;
Read only.
Example:
string ClassName = lp.TypeName;
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.