The PowerBuilder to .NET compiler changes the exception hierarchy used by the native PowerScript compiler.
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.
This figure shows the exception hierarchy for PowerBuilder applications in the .NET environment:
namespace ExceptionSample
{
// Custom exception class used in method second_test(int a) below
public class MyCustomException : Exception
{
public string GetMessage()
{
public string GetMessage()
{
return "Custom Error Thrown";
}
}
public class Test
{
public int division_test (int a)
{
int zero = 0;
// this will throw a System.DivideByZero exception
return a/zero;
}
public int second_test(int a)
{
a = a / 2;
throw new MyCustomException();
}
}
}
int i = 10
string ls_error
try
#IF Defined PBDOTNET Then
ExceptionSample.Test t
t = create ExceptionSample.Test
i = t.division_test(i)
#END IF
catch (DivideByZeroError e)
//the following lines would also work:
//catch (RuntimeError e)
//catch (Throwable e)
ls_error = e.getMessage ( )
MessageBox("Exception Error", ls_error)
end try
public class Test
{
public int second_test (int a)
{
a = a/2;
throw new MyUserException();
}
}
int i = 10
string ls_error
#IF Defined PBDOTNET Then
try
ExceptionSample.Test t
t = create ExceptionSample.Test
i = t.second_test(i)
catch (ExceptionSample.MyUserException e)
//this will also work: catch (System.Exception e)
ls_error = e.getMessage()
MessageBox("Custom Exception", ls_error)
end try
#END IF