SRL Archive - Help and Support

how does the "except" command work


weaselforce

2006-05-23 20:58:26

how does the "except" command work, Whenever I enter it into scar it becomes bold…i've never heard of this

jon buddy

2006-05-23 21:04:44

You put it at the end of a script:
end.
except
Then put anything here you want.

weaselforce

2006-05-23 21:06:36

…what? How is that useful whatsoever.

I just bugged Driger and got an answer.

Lig0r says:
Well, if something has the potential to cause an error, you put it in a try..except clause
Lig0r says:
try being the first thing to try. If that doesn't work it will do what comes after except

phantombmx

2006-05-23 21:13:16

var x, y: integer;

procedure Error;
begin
try
x:= 0/y;
Writeln(IntToStr(X));
except
Writeln('You just divided by 0');
end;
end;

begin
y:= 1;
Error;
y:= 0;
Error;
Writeln('We''re still running :)');
end.

driger1592

2006-05-23 21:24:26

Try..Except..Finally clauses are probably what you're looking for ;) I know I explained this to you on MSN, but I'm leaving this here for the community. If there's something that may go wrong in your script (such as cause a runtime error or math error or something to that effect) you can throw it into a Try..Except/Try..Finally/Try..Except..Finally clause. The example I like best is:
try
Sqrt(i);
except
Writeln('Cannot square root a negative number');
end;
Since the script runs into an error upon finding a negative number in a sqrt function, you can make sure it skips the error using the try..except clause. Instead of doing the sqrt, it will Writeln('Cannot square root a negative number');. However, if the sqrt function does not cause an error, it skips the except step. Now, you can do things in the clauses like so (note, no begin..ends needed, except for the end at the end of the clause):
try
Writeln('Only shown if there's no error');
Writeln('Note:');
Writeln('You can put as many things as you want here until the except/finally');
except
Writeln('Only shown if there was an error in the try section');
Writeln('Note:');
Writeln('You can put as many things as you want here until the finally/end');
finally
Writeln('This is shown whether or not there was an error in the try section');
Writeln('Note:');
Writeln('You can put as many things as you want here until the end');
end;
I hope this has helped. A section on this subject will be in the next version of my Tutorial Project ;)

Phantom, we're not stealing from Ben, are we :p XD

Dankness

2006-05-23 21:27:38

Try and Except are basicly Pascal Runtime Error Handling functions that were never documented in Scar, i belive Benland? was the first to actually discover how to use these and more information can be found by googling it. For the most part unless you are a advanced scripter you probably won't need to worry we them.

phantombmx

2006-05-23 21:27:44

no we arent :rolleyes: :p

BenLand100

2006-05-23 21:35:15

They are catching on :D Check out the source for my MMouse for a useable example. ;)

driger1592

2006-05-23 21:45:06

If memory serves (and it usually does), the first to show this was YourGrandpa back at Kaitnieks in his DectoBinorHex script ;)