Handling Exceptions in the .NET Environment – PB Docs 120


Handling Exceptions in the .NET Environment

The PowerBuilder
to .NET compiler changes the exception hierarchy used by the native
PowerScript compiler.

Modified exception hierarchy

In the native PowerBuilder environment, Throwable
is the root datatype for all user-defined exception and system error types.
Two other system object types, RuntimeError and Exception, inherit directly
from Throwable.

In the .NET environment, System.Exception is the root datatype.
The PowerBuilder to .NET compiler redefines the Throwable object
type as a subtype of the System.Exception class, and maps the .NET System.IndexOutOfRangeException
class to the PowerBuilder RuntimeError object type with the error
message �Array boundary exceeded.� The PowerBuilder
to .NET compiler also maps the following .NET exceptions to PowerBuilder
error objects:

  • System.NullReferenceException
    class to the NullObjectError object type

  • System.DivideByZeroException class to the DivideByZeroError
    object type

This figure shows
the exception hierarchy for PowerBuilder applications in the .NET
environment:

Exception hierarchy for PowerBuilder in the .NET environment

Handling Exceptions in the .NET Environment_1.gif

Example using a .NET system exception class

Even though a .NET exception class is mapped to a PowerBuilder
object type, you must use the PowerBuilder object type in your PowerScript
code. For example, suppose you define a .NET test class to test
for division by zero errors as follows:

To catch the error in PowerScript, you can use the DivideByZeroError
object type or either of its ancestors, RuntimeError or Throwable.
The following PowerScript code catches the error caused by the call
to the .NET Test class method for invoking division by zero errors:

Example using a custom .NET exception class

Suppose the .NET Test class is modified to catch a custom
.NET exception:

Because MyUserException is a user-defined exception in the
.NET environment, it cannot be caught by either the PowerBuilder
Exception or Throwable object types. It must be handled inside a
.NET conditional compilation block:


Document get from Powerbuilder help
Thank you for watching.
Was this article helpful?
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x