• Log in or sign up for an account here: https://www.sythe.org/login

[Question]How to use "Try...Except"?

Markizano

Active Member
Joined
Mar 2, 2006
Sythe Gold
0
Messages
100
Location
Aurora, CO
How do you use the "Try...Except" Procedure? In this function taken from the SRL includes. I found this code and wondered how the try and except thing is supposed to work.

Code:
function MMouse(dx, dy, RandX, RandY: Integer): Boolean;
var
  Path: TPointArray;
  i, cx, cy, Lx, Ly, xx, yy, Dist, MidRX, MidRY: Integer;
  Step, SlowDown: Extended;
begin
  Result := True;
  if (BenMouse) then
  begin
    try
      MidRX := Round(RandX / 2);
      MidRY := Round(RandY / 2);
      dx := dx + MidRX + NormDist(MidRX);
      dy := dy + MidRY + NormDist(MidRY);
      if dx < 0 then dx := 0;
      if dy < 0 then dy := 0;
      GetMousePos(cx, cy)
        if not ((Cx - dx = 0) and (Cy - dy = 0)) then
      begin
        Path := MakeMouseSplinePath(cx, cy, dx, dy, 7, 50, 40);
        Lx := cx;
        Ly := cy;
        for i := 0 to GetArrayLength(Path) - 1 do
        begin
          MoveMouse(Path[i].x, Path[i].y);
          Dist := Round(Sqrt((Path[i].x - dx) * (Path[i].x - dx) + (Path[i].y -
            dy) * (Path[i].y - dy)));
          if Dist < 50 then
          begin
            SlowDown := SlowDown + (0.05 * MouseSpeed);
          end;
          Wait(Round(((Random(2) + MouseSpeed) / 10) * Sqrt(Sqr(Path[i].x - Lx)
            + Sqr(Path[i].y - Ly)) + SlowDown));
          Lx := Path[i].x;
          Ly := Path[i].y;
        end;
      end;
      MoveMouseSmooth(dx, dy);
    except
      WriteLn('Error in BenMouse');
      WriteLn('Report: ' + IntToStr(cx) + ', ' + IntToStr(cy) + ' : ' +
        IntToStr(dx) + ', ' + IntToStr(dy) + ' : ' + IntToStr(mousespeed) +
          ';');
    end;
  end else
  begin // Start Mutant/RsN MMouse
    step := 4;
    xx := NormDist(RandX);
    yy := NormDist(RandY);
    dx := dx + xx;
    dy := dy + yy;
    GetMousePos(x, y);
    repeat
      if (Distance(x, y, dx, dy) < 100) then
        if (not (Distance(x, y, dx, dy) = 0)) then
          step := step - (10 / Distance(x, y, dx, dy));
      if (step < 1) then
        step := 1;
      x := x + Round((dx - x) / step);
      y := y + Round((dy - y) / step);
      MoveMouse(x, y);
      Wait(MouseSpeed);
    until (Distance(x, y, dx, dy) = 0);
  end;
end;
Somebody explain please...


Markizano
 
It tries to do something, if it can't then it will do what except is.

I use this in my flax picker:

Code:
try
    if (Players[CurrentPlayer].String2 = 'Both') then
      WriteLN('Picked And Spun: '+ IntToStr(StrToInt(Loads) * 28) + ' Flax');
    except
    end;
    try
    if (Players[CurrentPlayer].String2 = 'Flax') then
      WriteLN('Picked: '+ IntToStr(StrToInt(Loads) * 28) + ' Flax');
    except
    end;
    try
    if (Players[CurrentPlayer].String2 = 'Spin') then
      WriteLN('Spun: '+ IntToStr(StrToInt(Loads) * 28) + ' Flax');
    except
    end;
    try
    WriteLN('Grew: '+ IntToStr(i/2) +' Levels');
    except
    end;
    try
    if (Players[CurrentPlayer].String2 = 'Spin') then
      WriteLN('Gained: '+ IntToStr(StrToInt(Loads) * (28 * 15)) +' Crafting Experience');
    except
    end;

I have to do Try Except because sometimes Loads = '' which is not a vaild integer. If that does happen, it will end.
 
Code:
program New;

Procedure ThisWillWork;
Begin
  try
    Writeln('  Dividing 10 by 5, answer is:');
    Writeln('  '+IntToStr(10/5));
  except
    Writeln('  This is what will happen if the part in "try" couldnt work');
  finally
    Writeln('  This will happen whatever happens');
  end;
end;

Procedure ThisWontWork;
Begin
  try
    Writeln('  Dividing 10 by 0, answer is:');
    Writeln('  '+IntToStr(10/StrToInt('0')));
  except
    Writeln('  This is what will happen if the part in "try" couldnt work');
  finally
    Writeln('  This will happen whatever happens');
  end;
end;

begin
  ClearDebug;
  Writeln('OK, here we go...');
  Wait(2000);
  ThisWillWork;
  Wait(3000);
  Writeln('');
  Writeln('And now...');
  Writeln('');
  Wait(2000);
  ThisWontWork;
end.

See if that helps.
 
It does and I thank you. Now I am at my next question, how would I get something like this to work in VB6? I can tell from looking at it that it wouldn't be a sub/procedure, function, or property. It would have to be a whole new kind of way to program it. If...Then definitely has something to do with it, but exactly how I would go about doing that really boggles me. If nobody knows, then don't worry about it, just seeing if i could advance my knowledge of programming a bit.

Off Topic: Who thinks that this was the first decent question about SCAR in a long time (Nothing stupid like "where's the dl link to scar" or "how do i install SRL" as I have consistently seen)?
 
Cool, looks interesting....
 
Runescape Gamblingrunescape gambling
Back
Top