PDA

View Full Version : Runtime errors bully? Try - Except is the answer for your problems.



n3ss3s
08-01-2007, 04:08 PM
As the topic says.
Ever seen when you come from school, wake up in teh morning and your scar says something about runtime error and does nothing?
To avoid that, the creator of programming(who might that be?) has given us...

Try Except

There is 2 ways to get a script running good.
a) script right
b) script easy, right, and lazy.

The how to do the a option would be longer explanation, so I will explain the b one, the meaning of this tutorial - Try Except.

1. What is try except supposed to do?
Avoid runtime errors!

2. How to use it

Well, I'll make an example instead of explanation.

program new;
var
A: array of Integer;
begin
SetArrayLength(A, 3); // from 0 to 2
A[3] := 15; // We are trying to edit the "3" when "2" is the maximum
end.

That program would give a runtime error when ran.

To avoid that we could use try except and do


program new;
var
A: array of Integer;
begin
SetArrayLength(A, 3); // from 0 to 2
try
A[3] := 15; // We are trying to edit the "3" when "2" is the maximum
except
Writeln('Oh noes! We tried to edit array slot which is out of range!');
end;
end.

Basically what that script does, is sets array "A" length to 3 which means the thingys are 0, 1, 2 and we are trying to edit the 3 which doesnt exist.

Try part of try catch, tries to execute the part of the program between try and catch, its a bit like begin and end.

The part between except and end executes the part of the program which we have coded to do when our thing (in this case, the A[3] := 15) could not be executed.

In this case, it would write "Oh noes! We tried to edit array slot which is out of range!" to the scar debug box.

But, as many of you may think, if not, it ofcourse does not execute both the try and except part, because that'd be too leet.

This is pretty simple thing so I think I dont need to explain more.

Take the example code and play with it... (modify and learn)...

Thx

passiondrive
08-01-2007, 06:31 PM
Great tut.

3Garrett3
08-01-2007, 06:47 PM
Umm, ya, I have been fiddling with these things for a bit, and could you think of some other way to use this, like in a real rs script?

n3ss3s
08-01-2007, 07:04 PM
Is it okay if I pick a procedure from my unfinished draynor script?


Well lets see, my uhber leet fishing procedure uses it

function Fish: Boolean;
var
FMark, F: Integer;
A, Found, LoopRandomLO: Boolean;
begin
repeat
if(FindSymbol(x, y, 'fish'))then
begin
Mouse(x, y, 3, 3, True);
FFlag(0);
repeat
for F := 1 to 3 do
begin
case F of
1: A := FindColorTolerance(x, y, Fc[F], MSX1, MSY1, MSX2, MSY2, 10);
2: A := FindColorTolerance(x, y, Fc[F], MSX1, MSY1, MSX2, MSY2, 10);
3: A := FindColorTolerance(x, y, Fc[F], MSX1, MSY1, MSX2, MSY2, 10);
end;
Wait(100 + Random(25));
end;
if (A) then
begin
MMouse(x, y, 2, 2);
if (IsUpText('Net')) then
begin
Found := True;
try
Fc[F] := GetColor(x, y);
except
Writeln('Lol, runtime error would´ve come if we´d update color');
Writeln('Well, theres nothing wrong with our colors since we could');
Writeln('Find the fishing spot :)');
end;
Click(True);
MarkTime(FMark);
end;
end;
if(RandomLoggedOut)then
begin
Writeln('*Sigh* Logged out due to a random');
NextPlayer(False);
LoadISM;
SendInterScarMessage(ISM[ISMno], 'Break');
ISMno := ISMno + 1;
LoopRandomLO := True;
end;
if(not(LoggedIn)) and (not(RandomLoggedOut)) then
begin
Writeln('We are logged out. Not due to a random.');
Writeln('Player stays active, but nextplayer comes.');
NextPlayer(False);
LoadISM;
SendInterScarMessage(ISM[ISMno], 'Break');
ISMno := ISMno + 1;
end;
until (Found) or(LoopRandomLO) or (not(LoggedIn));
end;
while (TimeFromMark(FMark) < 20000) do
begin
try
NontiBan;
Wait(100 + Random(25));
FasterRandoms;
Wait(100 + Random(25));
HandleWhirlPool;
Wait(100 + Random(25));
if(FindFight)then
begin
RunAwayDirection('w');
Wait(8500 + Random(250));
RunBack;
end;
if(not(FindDeformed(x, y, SmallNet, MIX1, MIY1, MIX2, MIY2)))
or(FindNpcChatText('You need a net')) then
begin
FindNet(x, y);
MMouse(x, y, 1, 1);
if(IsUpText('Small'))then
begin
Click(True);
end else
begin
MMouse(x, y, 0, 0);
if(IsUpText('Small'))then
begin
Click(True);
end else
begin
Writeln('Could not find your fishing equipment, changing player');
NextPlayer(False);
LoadISM;
SendInterScarMessage(ISM[ISMno], 'Break');
ISMno := ISMno + 1;
end;
end;
end;
except
Writeln('There was some crap in my Antiban and antirandoms');
Writeln('Problem handled with Try - Except. ');
end;
end;
GameTab(4);
if(InvCount = 27) then
begin
Result := True;
end;
until(not(LoggedIn)) or (Result);
end;

3Garrett3
08-01-2007, 07:09 PM
But, with the get color thing, I dont see how a runtime error would occur, unless you messed up and set the array length wrong.

n3ss3s
08-01-2007, 07:25 PM
No, its FC: array[1..3] of Integer
but anyways, thats not the point, u know what I mean and how to use :)

p.s: other than that its a pretty pwnz0r fishing proc :)

3Garrett3
08-01-2007, 07:29 PM
Ya, it is, but still, I guess I just know how to script "right" because I dont get runtime errors, and have never used Try or Except.

Smartzkid
08-01-2007, 07:40 PM
I'll try to fish out an example from one of my scripts.

Some things just don't always work right, and it's quicker to use a try-except than to re-script the entire function to not give errors.

n3ss3s
08-01-2007, 07:47 PM
I'll try to fish out an example from one of my scripts.

Some things just don't always work right, and it's quicker to use a try-except than to re-script the entire function to not give errors.
I was just about to say that.

One question: Does "you beat me in to it" mean that I was just about to say that or sumting like...

3Garrett3
08-01-2007, 08:04 PM
Ya, it does.

n3ss3s
08-01-2007, 08:17 PM
k thx

Miitchyy
08-04-2007, 10:48 AM
You could use this in DeclarePlayers so all the noobs dont compain for out of range errors =]