PDA

View Full Version : Avoiding runtime errors and handling them



Cazax
06-22-2009, 04:10 AM
Have you ever got a runtime error?
[Runtime Error] : Exception: blablabla

Well there are unknown functions in SCAR that gives what type of runtime error was, what was the error, the invalid parameter and what procedure/function caused it.

function ExceptionParam: string;
function ExceptionPos: LongWord;
function ExceptionProc: LongWord;
function ExceptionType: TIFException;
function ExceptionToString(err : TIFException, Param : String): string;
Don't worry, LongWord's are just 32 bit integers that can only hold positive numbers :)

You might wonder what to do with them, well it's really simple:

This is a normal main loop:

begin
//code here
end.

But to catch those annoying runtime errors you need to use the Try, Except and Finally block:

begin
Try
//Code here
Except
//Exception
end.

Now that we have it ready, add the exception handler:

begin
Try
/Code here
Except
Writeln('Error number ' + IntToStr(ExceptionPos) + ', procedure number ' + IntToStr(ExceptionProc) + ', Reason: ' + ExceptionToString(ExceptionType, ExceptionParam));
End;
end.

Some examples:
program New;
var
a : integer;

procedure err;
begin
a := a / (a-a);
end;

begin
Try
err;
Except
Writeln('Error number ' + IntToStr(ExceptionPos) + ', procedure number ' + IntToStr(ExceptionProc) + ', Reason: ' + ExceptionToString(ExceptionType, ExceptionParam));
End;
end.
Output:
Error number 56, procedure number 1, Reason: divide by Zero

program New;
var
a, b, c : integer;

procedure err;
begin
FindBitmap(a, b, c);
end;

begin
Try
err;
Except
Writeln('Error number ' + IntToStr(ExceptionPos) + ', procedure number ' + IntToStr(ExceptionProc) + ', Reason: ' + ExceptionToString(ExceptionType, ExceptionParam));
End;
end.
Output:
Error number 64, procedure number 1, Reason: Exception: Access violation at address 006585D8 in module 'scar.exe'. Read of address 00000000


Have fun catching runtime errors :)

Macro_FTW
06-23-2009, 06:35 AM
Nice tutorial. I've been looking for how to do this for a while. :P

~Macro_Ftw

zenma
08-11-2009, 02:23 PM
Thanks for this, I had no clue SCAR had support for exception handling. Now my script can run without the worry of runtime exceptions :)