What's the proper way to use ABORT or ABORT QUOTE?

If you have any questions, remarks, ... if you need help... its here...

What's the proper way to use ABORT or ABORT QUOTE?

Postby bobgillies » 23 Feb 2017 01:39

When I try this test,

tvar: queenCount 1 to queenCount
tvar: queenRank 1 to queenRank

: test queenCount queenRank == if "\nAll the queens solved." . abort then ;

test

It branches to the IF clause, prints the message and aborts, but the abort throws an exception along with it. I only want to abort the program to the REPL with an ABORT QUOTE.

How do I do an ABORT or ABORT QUOTE without the additional exception thrown message?
bobgillies
 
Posts: 60
Joined: 24 Jan 2017 06:26

Re: What's the proper way to use ABORT or ABORT QUOTE?

Postby Franck » 23 Feb 2017 12:37

abort is used to throw an exception and need a string as parameter.

abort \ string --

It is used when something went wrong and the processus can't continue.
Unless the exception is catched before, it will be catched by the repl that will print it.

In your case, I'am not sure that throwing an exception is the best way to handle the end of of the computation.
It seems that the test correspond to a normal end (All the queens solved) rather than an exceptional event.

You could use "return" to return from a word before the end of the word :

Code: Select all
: test
   queenCount queenRank == if "\nAll the queens solved." . return then
   "\nStill work to do" .
;


In your case, an exception is raised by the REPL because abort needs a string.
But even if you provide a string, it will be printed as an exception :

Code: Select all
: test
   queenCount queenRank == if
      "\nAll the queens solved." .
      "Job done" abort
      then
   "\nStill work to do" .
;




If you really want to raise an exception (but, in this case, I don't suggest you to do it), and want to catch it before the REPL, you will have to write a try block (see the manual for details) :

Code: Select all
: test
   queenCount queenRank == if
      "\nAll the queens solved." .
      "Job done" abort
      then
   "\nStill work to do" .
;

: test1
| e |
   try: e [ test ] when: [ "\n I catched an exception" . ]
   "End of test1" .
;


Franck
Franck
 
Posts: 140
Joined: 29 Oct 2014 19:01

Re: What's the proper way to use ABORT or ABORT QUOTE?

Postby bobgillies » 23 Feb 2017 22:24

Hi Franck, I see where I went wrong terminating the program should not use ABORT for a successful early termination. I should have instead used RETURN for a successful early termination of the program.

Bob

edit: I also see you're teaching me the way to go about Throwing and Catching exceptions using TRY: and When:.
Thank you. :)

Bob
bobgillies
 
Posts: 60
Joined: 24 Jan 2017 06:26

Re: What's the proper way to use ABORT or ABORT QUOTE?

Postby Franck » 24 Feb 2017 01:45

Right.

But "return" does not terminate the program : it returns from the current function (or method) and immediatly go back to the caller of this function.

Franck
Franck
 
Posts: 140
Joined: 29 Oct 2014 19:01


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron