A biiiig break ?

O

Olivier

Dear all,

I would like to find an instruction that
stops n level of my programm ...
Typically, I have a gtk-graphical application (a game),
the user asks for something (say function do_that)
and something wrong happens
so I would like the program to abort the current computation
(end of do_that)and go back to its usual "waiting" state

Any inkling on how such a behaviour should be coded ?
Best,
Amities,
Olivier
 
K

Keith Thompson

Olivier said:
I would like to find an instruction that
stops n level of my programm ...
Typically, I have a gtk-graphical application (a game),
the user asks for something (say function do_that)
and something wrong happens
so I would like the program to abort the current computation
(end of do_that)and go back to its usual "waiting" state

Sounds like a job for setjmp() and longjmp(). See your C textbook for
details.
 
M

Malcolm

Olivier said:
Thanks!! That sounds good. I'll experiment and come back if ...
Cheers, O.
Keith Thompson's advice isn't wrong as such, but it is fairly unusual
to see setjmp(0 and longjmp() in C code. It breaks the normal flow of the
language.

C++ exception handling uses a similar mechanism, but is much better because
items on the stack are properly destroyed. You might consider going to C++.
C isn't the best language for absolutely every problem.

Another way is to make all your function return an "abort" code. The
function that detects the error aborts and returns -1, the caller detects it
and returns -1 itself, and so on until control goes back where it wants to
be.

Another techniques is to use "sticky errors". Let's say the function is to
draw some complex graphics on an image. In your image structure, put an
"error" flag, set wheneve someone tries to draw a pixel out of bounds,
assuming this is your error.

Drawing can physically continue, but if the previous operation was an error
then the subsequent drawing is invalid (if you can't draw the yellow circle
correctly, then putting on the two dots and smile isn't going to work). Low
level functions don't need to worry about this. When the high level function
examines the image, it sees that the error flag is set. The yellow circle
wasn't drawn, and the eyes and mouth have probably messed up somethig else.
So it simply discards the whole image.
 
O

Olivier

Thanks.

What I'm really looking for is the emacs lisp way
of handling errors :
(catch 'this-flag
function
function-apply-if-this-flag-has-been-caught)

and function reads
(defun function
....
if(error) {throw 'this-flag};
)

When the throw command is issued, the catch
process is applied. That way errors are clearly
identified.

Maybe I'll do that with gotos finally, since setjmp
is too strong: something have happened that I don't
erased! Well, for instance the last message to the
user telling him/her what is happening :)

catch-throw give a well-defined surrounding for
these gotos, in a somewhat encapsulated world.
Well, well, emacs-lisp is programmed in C if I
remember well, maybe I should go and look at the
way it is coded there :)

Cheers,
Olivier
 
C

Chris Torek

What I'm really looking for is the emacs lisp [catch and throw] ...

To implement catch, throw, and unwind-protect, there is a great deal
of machinery embedded in a Lisp system. C lacks this machinery.

You can attempt to simulate it with setjmp and longjmp, but this
requires a lot of care. A "call" to the longjmp() macro is the
"goto" they tell you to avoid in beginning programming classes. :)
Worse, "longjmp" invalidates the values of many variables, requiring
you to sprinkle your code liberally with "volatile" modifiers.

C++ also has catch and throw (and most of the machinery), but lacks
an explicit unwind-protect. (You can fake the last by using an
automatic variable instance of a class object that has a destructor,
but you lose some control over order-of-execution.)
 
R

Robert Gamble

Chris said:
What I'm really looking for is the emacs lisp [catch and throw] ...

To implement catch, throw, and unwind-protect, there is a great deal
of machinery embedded in a Lisp system. C lacks this machinery.

You can attempt to simulate it with setjmp and longjmp, but this
requires a lot of care. A "call" to the longjmp() macro is the
"goto" they tell you to avoid in beginning programming classes. :)

setjmp is a macro, longjmp is a function.

Robert Gamble
 
O

Olivier

To implement catch, throw, and unwind-protect, there is a great deal
of machinery embedded in a Lisp system. C lacks this machinery.

Aie. I'm surprised at that, but I'm so used to the flexibility
of lips :-( Except that for graphical stuff, or linear programming,
well I can't quite avoid C: it is too used nowadays :)

So I'll implement a by-hand error control with states and so on :-(
Thanks !
O.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,166
Messages
2,570,901
Members
47,442
Latest member
KevinLocki

Latest Threads

Top