|
PortController.NET
About PortController Errors
PortController notifies your application of any errors it encounters by throwing an exception with a description of the error. The following examples show how to handle these exceptions in your code.
Note that, for simplicity, the examples in the reference section do not include error handling constructs. You may examine the Test Panel sample project for examples of error handling in Visual Basic, C#, and C++.
Visual Basic
PortController errors are handled in Visual Basic using either structured exception handling similar to C# and C++, or by using the familiar unstructured On Error Goto construct. When using structured handling, you must specifically reference the Exception variable as type System.Exception. This is because PortController.NET includes "Windows.h", which globally defines its own exception type and Visual Basic's indifference to case will cause it to default to the exception type. The following example traps an error and displays the error message using structures exception handling:
Try
myPortController.Open("COM1")
Catch ex As System.Exception
MsgBox(ex.Message)
End Try
When using unstructured (VB6 style) exception handling, detailed error information can be found in the application's global Err object. For example, the following code traps an error and displays a description in a message box:
Private Sub MySub()
On Error GoTo MyErrorHandler
myPortController.Open "COM1"
Exit Sub
MyErrorHandler:
MsgBox(Err.Description)
End Sub
C#
PortController errors are handled in C# using standard try/catch blocks. The following code traps an error and displays a description in a message box:
try
{
myPortController.Open("COM1");
}
catch(Exception Ex)
{
MessageBox.Show(Ex.Message);
}
C++
PortController errors are handled in C++ using standard try/catch blocks. The following code traps an error and displays a description in a message box:
try
{
myPortController->Open("COM1");
}
catch(Exception *Ex)
{
MessageBox::Show(Ex->Message);
}
|