The creation of a connection to an AppServer should be wrapped with exception-handling code to avoid memory leaks and unnecessary allocation of client and server resources. Also, information received from a caught exception can be valuable in tracing and fixing connection errors.
The following is an example of handling connection exceptions:
try
{
//Create Customer AppObject to connect
Customer appObj = new Customer(ConnectObj);
}
catch (Progress.Open4GL.Exceptions.ConnectException ex)
{
MessageBox.Show(ex.ToString( ));
}finally
{ if (appObj != null)
{
appObj.Dispose( );
}
}
This example executes the connect in the try block. If there is an exception thrown by the process, the catch block displays the exception message to the user.
The catch block is specifically catching the ConnectException. For more information on .NET Open Client exceptions, see Handling Errors.
The finally block ensures that the Dispose( ) method is called on the AppObject. By putting the Dispose( ) method in the finally block, you ensure that the connection is closed whether an exception occurs or the connect is successful.