Exception handling and debugging

To handle errors, you use the error codes returned from PBNI methods. Some functions of the IPB_Session interface return detailed error codes that make debugging easier.

Native methods, such as the IPBX_UserObject Invoke method, return either PBX_OK or PBX_FAIL if the extension encounters a serious problem from which it cannot recover.

Whenever the PowerBuilder VM gets PBX_FAIL from a native method, it throws a PBXRuntimeError in the PowerBuilder application. PBXRuntimeError inherits from the PowerBuilder RuntimeError system object and can be caught and handled in a script in the same way as any exception in PowerBuilder.

To catch these errors, wrap your PowerScript call in a try-catch block as follows:

TRY
   n_cpp_pbniobj    obj
   obj = CREATE n_cpp_pbniobj
   obj.of_test( arg1 )
CATCH ( PBXRuntimeError re )
   MessageBox( "Caught error", re.getMessage() )
END TRY

The IPB_Session interface provides a set of methods to handle exceptions that occur in native code. Use HasExceptionThrown to determine whether an exception occurred. If it did, use GetException to get the current exception object so that it can be handled. If necessary, you can throw exceptions to PowerBuilder with ThrowException. When an exception has been handled, use ClearException to clear it.

Debugging

You cannot edit a native class in the PowerBuilder development environment, and you cannot enter native methods in the PowerBuilder debugger because the methods are C++ methods. You must use a C/C++ debugger to debug an extension module.