OPCDA.NET Reference Manual
Error Handling
Send comments on this topic.
OPCDA.NET Client Component > Application Development > Error Handling

Glossary Item Box

OPCDA.NET allows the application to handle errors in two ways:

 

Return Codes

In the classic mode most errors are reported as function return codes.
The application has to check the result code returned by OPCDA.NET methods and do the required error handling.

Errors occurring in system modules such as e.g. DCOM throw exceptions independent of the error handling mode of OPCDA.NET. Therefore, the application has to catch and handle exceptions. Typically the occurring exceptions signal non-recoverable error conditions and the application only needs a catch block on an outer level.

Some OPCDA.NET methods return a function value other than a HRESULT error code. Errors in such methods are rare but not impossible. In case of error these methods throw an OPCException. A sample of such a method is AddGroup.

OpcServer Srv ;
OpcGroup Grp ;
private void btnConnect_Click(object sender, System.EventArgs e)
{
   int rtc ;
   try
   {
      Srv = new OpcServer() ;
      rtc = Srv.Connect( cbServerName.Text );
      if( HRESULTS.Failed(rtc) )  // connect failed
      {
         tbSrvStatus.Text = "Connect Error " + rtc.ToString();
         Srv = null ;
         return;
      }
      Grp = Srv.AddGroup( "", true, 100, 1 );
   }
   catch( Exception ex )
   {
      tbSrvStatus.Text = ex.Message ;
      Srv = null;
      return;
   }
}
			
Private Srv As OpcServer
Private Grp As OpcGroup
Private Sub btnConnect_Click(ByVal sender As Object, _
                  ByVal e As System.EventArgs)
   Dim rtc As Integer
   Try
	  Srv = New OpcServer()
	  rtc = Srv.Connect(cbServerName.Text)
	  If HRESULTS.Failed(rtc) Then ' connect failed
		 tbSrvStatus.Text = "Connect Error " + rtc.ToString()
		 Srv = Nothing
		 Return
	  End If
	  
	  Grp = Srv.AddGroup("", True, 100, 1)
	  
   Catch ex As Exception
	  tbSrvStatus.Text = ex.Message
	  Srv = Nothing
	  Return
   End Try
End Sub
			

 

 

 

Exceptions

All errors throw an exception.
The boolean property ErrorsAsExecptions in the OpcServer object has to be set to true to enable this mode.

The application can handle all kind of errors in the same way. To handle recoverable errors, catch blocks may be needed on multiple levels. Exceptions in OPCDA.NET are thrown as OPCExceptions to enable access to the HRESULT error code.

OpcServer Srv ;
OpcGroup Grp ;
try
{
   Srv = new OpcServer() ;
   Srv.ErrorsAsExecptions = true ;
   Srv.Connect( cbServerName.Text );
   Grp = Srv.AddGroup( "", true, 100, 1 );
}
catch( OPCException ex )
{
   tbSrvStatus.Text = "Error 0x" + ex.hResult.ToString("X") 
		      + ":  " + ex.Message ;
   Srv = null;
   return;
}

 catch( Exception ex )      // handling COM exceptions
{
   tbSrvStatus.Text = ex.Message ;
   Srv = null;
   return;
}
Private Srv As OpcServer
Private Grp As OpcGroup
Try
   Srv = New OpcServer()
   Srv.ErrorsAsExecptions = True
   Srv.Connect(cbServerName.Text)
   Grp = Srv.AddGroup("", True, 100, 1)

Catch ex As OPCException
   tbSrvStatus.Text = "Error 0x" + ex.hResult.ToString("X") _
                      + ":  " + ex.Message
   Srv = Nothing
   Return

 Catch ex As Exception ' handling COM exceptions
   tbSrvStatus.Text = ex.Message
   Srv = Nothing
   Return
 End Try

In some cases it is anyway necessary to check the function code. Some method return more than one success code. E.g. the Read method may return

S_OK The function executed successfully
S_FALSE The function was only partially successful. The returned error array has to be checked to find which items returned an error indication.


SyncIOGroup Grp ;
ItemDef	    ItemData ;

public void ReadOneItem()
{
   OPCItemState Rslt ;
   OPCDATASOURCE dsrc = OPCDATASOURCE.OPC_DS_CACHE ;
   try
   {
      int rtc = Grp.Read( dsrc, ItemData, out Rslt );

      if( HRESULTS.Succeeded(rtc) // item read successful
      {
         tbValue.Text = Rslt.DataValue.ToString();
         tbError.Text = "";
      }
      else			// the item could not be read
      {
         tbValue.Text = "";
         tbError.Text = Grp.GetErrorString( Rslt.Error );
      }
   }
   catch( Exception ex )
   {
      tbError.Text   = ex.Message ;
   }

Private Grp As SyncIOGroup
Private ItemData As ItemDef

Public Sub ReadOneItem()
   Dim Rslt As OPCItemState
   Dim dsrc As OPCDATASOURCE = OPCDATASOURCE.OPC_DS_CACHE
   Try
	  Dim rtc As Integer = Grp.Read(dsrc, ItemData, Rslt)

	  If HRESULTS.Succeeded(rtc Then ' item read successful
		 tbValue.Text = Rslt.DataValue.ToString()
		 tbError.Text = ""
	  Else ' the item could not be read
		 tbValue.Text = ""
		 tbError.Text = Grp.GetErrorString(Rslt.Error)
	  End If
   Catch ex As Exception
	  tbError.Text = ex.Message
   End Try
End Sub

 

Copyright © 2002-2018 Advosol Inc. All Rights Reserved.