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.
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:
This figure shows
the exception hierarchy for PowerBuilder applications in the .NET
environment:
Example using a .NET system exception class
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:
1 |
public class Test |
1 |
{ |
1 |
public int division_test (int a) |
1 |
{ |
1 |
return a/0; |
1 |
//pops a System.DivideByZero exception |
1 |
} |
1 |
} |
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:
1 |
int i = 10 |
1 |
string ls_error |
1 |
try |
1 |
#IF Defined PBDOTNET Then |
1 |
Test t = create Test |
1 |
i = t.division_test(i) |
1 |
#END IF |
1 |
catch (DivideByZeroError e) |
1 |
//the following lines would also work: |
1 |
//catch (RuntimeError e) |
1 |
//catch (Throwable e) |
1 |
ls_error = e.getMessage ( ) |
1 |
end try |
Example using a custom .NET exception class
.NET exception:
1 |
public class Test |
1 |
{ |
1 |
public int second_test (int a) |
1 |
{ |
1 |
a = a/2; |
1 |
throw new MyUserException(); |
1 |
} |
1 |
} |
.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:
1 |
int i = 10 |
1 |
string ls_error |
1 |
#IF Defined PBDOTNET Then |
1 |
try |
1 |
Test t = create Test |
1 |
i = t.second_test |
1 |
catch (MyUserException e) |
1 |
//this will also work: catch (System.Exception e) |
1 |
ls_error = e.getMessage() |
1 |
end try |
1 |
#END IF |