PDA

View Full Version : [Tut] Using Custom Errors



LordJashin
03-11-2012, 09:54 PM
Custom Errors:

program CustomError;

begin

RaiseException(erCustomError, 'Epic fail');

WriteLn('haha you failed'); // This line never executes

end


This is what happens in Simba:

http://i40.tinypic.com/35ku6bc.png


Explored example:

program CustomError;

var
ThrowFail : Boolean;
Error: String;

// Some might make their own procedure for Script Termination
procedure FailThem;
begin
WriteLn('Failed to: ' + Error);
//TerminateScript;
end;

begin

Error := 'Find Bitmap';

FailThem;

// Someone might Terminate a script like so

ThrowFail := true;

if ThrowFail then
begin
WriteLn('Failed to $(#*&@%^)$#^%$#');
WriteLn('Terminating Script');
//TerminateScript;
end;

(* Doing this also Terminates the script but has advantages, the
line number is given and highlighted, and using this does a WriteLn
only use this though if there's an error, and your script cannot go
on because of it *)

// This will output: Error: Epic fail at line 35 if ThrowFail is True

if ThrowFail then
RaiseException(erCustomError, 'Epic fail');

WriteLn('haha you failed'); // This line never executes

Dervish
03-11-2012, 09:56 PM
Okay, pictures and so but... What use ?

masterBB
03-11-2012, 10:11 PM
Okay, pictures and so but... What use ?

You can use it for debugging purposes. Like this:

if SomethingGoesTerribleWrong then
RaiseException(erCustomError, 'Oh no, it went wrong');

Note that is does display the line at which the error occurred.

Brandon
03-11-2012, 10:12 PM
Okay, pictures and so but... What use ?

It's a very short tut but very useful.. I do this in the waterfiends incase people do not have the right maps.. It stops a script from running..

It's almost the same as making your own procedure to shutdown and terminate the script.. except that it'll highlight the line where the problem has occured..

Usually NOT made for scripts but actually for Simba itself as this exception raising is for Runtime Compiling.. So Iunno if he's using it right or not..

Usually you want to raise an exception when the user pressed play and something is wrong.. Not while the script is running or else it can actually in some cases, cause a user to die if it's a fighting script as it will just terminate on the spot!

It's the exact reason that we use Try-Catch statements.. so that we can avoid pre-termination.. especially if u try to free a dtm that is already free or doesn't exist..

Advice.. Only do this at the start of a script IF needed..

LordJashin
03-11-2012, 10:57 PM
It's a very short tut but very useful.. I do this in the waterfiends incase people do not have the right maps.. It stops a script from running..

It's almost the same as making your own procedure to shutdown and terminate the script.. except that it'll highlight the line where the problem has occured..

Usually NOT made for scripts but actually for Simba itself as this exception raising is for Runtime Compiling.. So Iunno if he's using it right or not..

Usually you want to raise an exception when the user pressed play and something is wrong.. Not while the script is running or else it can actually in some cases, cause a user to die if it's a fighting script as it will just terminate on the spot!

It's the exact reason that we use Try-Catch statements.. so that we can avoid pre-termination.. especially if u try to free a dtm that is already free or doesn't exist..

Advice.. Only do this at the start of a script IF needed..

And here I thought this was self explainatory :duh:. You could use this like for if Statements, to stop the script at that point and show an error. Might be useful, example:


program CustomError;

var
ThrowFail : Boolean;
Error: String;

// Some might make their own procedure for Script Termination
procedure FailThem;
begin
WriteLn('Failed to: ' + Error);
//TerminateScript;
end;

begin

Error := 'Find Bitmap';

FailThem;

// Someone might Terminate a script like so

ThrowFail := true;

if ThrowFail then
begin
WriteLn('Failed to $(#*&@%^)$#^%$#');
WriteLn('Terminating Script');
//TerminateScript;
end;

(* Doing this also Terminates the script but has advantages, the
line number is given and highlighted, and using this does a WriteLn
only use this though if there's an error, and your script cannot go
on because of it *)

// This will output: Error: Epic fail at line 35 if ThrowFail is True

if ThrowFail then
RaiseException(erCustomError, 'Epic fail');

WriteLn('haha you failed'); // This line never executes

end.