473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

How to exit out of a function ? what is try-catch-throw in terms of Program Counter

I have some code like this:

(if (test)
(exit)
(do something))
or

(if (test)
( do something)
(exit))
Various levels of nestings.

I have several questions, basic to sophisticated.

(1) What is the lisp equivalent idiom for (exit) as in bash or
in C.
(2) What is the best practice to handle this kind of problems?

(3) What is the intermediate practice to handle this kind of
problems.

NOTE: I am really afraid of try-catch-throw. I have never been
able to understand it since it does not exist in C and I cant
really visualize the construct in terms of C. That is what my
brain can process. If you understand it so well, you can show
me how one would really implement that kind of construct in
C and then by extension I can see that kind of program flow
in LISP. Whether its imperative programming or functional,
beneath there is program counter and assembly. C is close
to machine so much that it is almost assembly. So understanding try-c-
t in C is equivalent to understanding at
the level of machine language.

I therefore take the liberty to crosspost in C and C++ groups.

Oct 20 '07 #1
28 3758
On Oct 20, 1:45 pm, gnuist...@gmail.com wrote:
I have some code like this:

(if (test)
(exit)
(do something))

or

(if (test)
( do something)
(exit))

Various levels of nestings.

I have several questions, basic to sophisticated.

(1) What is the lisp equivalent idiom for (exit) as in bash or
in C.
(2) What is the best practice to handle this kind of problems?

(3) What is the intermediate practice to handle this kind of
problems.

NOTE: I am really afraid of try-catch-throw. I have never been
able to understand it since it does not exist in C and I cant
really visualize the construct in terms of C. That is what my
brain can process. If you understand it so well, you can show
me how one would really implement that kind of construct in
C and then by extension I can see that kind of program flow
in LISP. Whether its imperative programming or functional,
beneath there is program counter and assembly. C is close
to machine so much that it is almost assembly. So understanding try-c-
t in C is equivalent to understanding at
the level of machine language.

I therefore take the liberty to crosspost in C and C++ groups.
Is there any explanation of try-catch-throw in terms of
gotos than PC ? Is that explanation more simpler ?
Oct 20 '07 #2
<gn*******@gmail.comwrote in message
news:11**********************@k35g2000prh.googlegr oups.com...
>I have some code like this:

(if (test)
(exit)
(do something))
or

(if (test)
( do something)
(exit))
Various levels of nestings.

I have several questions, basic to sophisticated.

(1) What is the lisp equivalent idiom for (exit) as in bash or
in C.
Your message post is asking "How to exit out of a function..." yet you are
showing code that almost exits out of the program. exit() would do that.

If you wish to exit out of the function then the correct keyword is return.
Such as:

if (condition)
return;
(do something)

if ( condition )
(do something )
return;

return can return a value if the function returns a value, such as
return 1;
return foo;
return bar();
etc..
(2) What is the best practice to handle this kind of problems?
It depends on coding style. There is some coding style that states that any
function will only have one return point. In which case you would do:

if ( condition )
( do something )
return;

Personally, I chose to return early if it's what I would consider an error
condition.

if ( condition )
return NULL;
( do something )
return Value;

There really is no best way, it depends on coding standard and how easy the
code is to write, read and maintain.
(3) What is the intermediate practice to handle this kind of
problems.
Again, it depends on the problems. At different times in my career I have
gone with returning whenever I've wanted to to returning only at the very
end.
I find that huge if statments, however, make my code harder to write, read
and maintain, so I will check for conditions early and return early if I
can. I try not to return in the middle of a function, but only at the top
or bottom. But it depends on the situation.
NOTE: I am really afraid of try-catch-throw. I have never been
able to understand it since it does not exist in C and I cant
really visualize the construct in terms of C. That is what my
brain can process. If you understand it so well, you can show
me how one would really implement that kind of construct in
C and then by extension I can see that kind of program flow
in LISP. Whether its imperative programming or functional,
beneath there is program counter and assembly. C is close
to machine so much that it is almost assembly. So understanding try-c-
t in C is equivalent to understanding at
the level of machine language.

I therefore take the liberty to crosspost in C and C++ groups.
As for try...catch blocks, I try to reserve those only for actual errors.
One reason I have found I need to use them when a function is supposed to
return a reference to an object, and that object doesn't exist. I have to
return something, and something is not good enough. So I get around it by
throwing out of the function.

(Untested code)

Player& FindPlayer( const std::string& Name, std::map<std::string, Player)
{
std::map<std::string, Player>::iterator it = Player.find( Name );
if ( it == Player.end() )
{
// Player was not found in map. Have to return something. Lets just
throw.
throw std::exception("Player not found");
}
return (*it).second;
}

int main()
{
// ...
try
{
Player ThisPlayer& = FindPlayer( SomePlayer );
// use ThisPlayer
}
castch ( std::exception e )
{
std::cout << "Player " << SomePlayer << " not found.\n";
}
}
Oct 21 '07 #3
gn*******@gmail.com writes:
I have some code like this:

(if (test)
(exit)
(do something))
or

(if (test)
( do something)
(exit))
That's Lisp, yes? Saying so would be good, since we naturally assume
that anything posted to comp.lang.c is C.
Various levels of nestings.

I have several questions, basic to sophisticated.

(1) What is the lisp equivalent idiom for (exit) as in bash or
in C.
You're expecting C programmers to know what (exit) does in Lisp?

[snip]
I therefore take the liberty to crosspost in C and C++ groups.
That's rarely a good idea.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 21 '07 #4

<gn*******@gmail.comwrote in message
news:11**********************@k35g2000prh.googlegr oups.com...
>I have some code like this:

(if (test)
(exit)
(do something))
or

(if (test)
( do something)
(exit))
Various levels of nestings.

I have several questions, basic to sophisticated.

(1) What is the lisp equivalent idiom for (exit) as in bash or
in C.
(2) What is the best practice to handle this kind of problems?

(3) What is the intermediate practice to handle this kind of
problems.

NOTE: I am really afraid of try-catch-throw. I have never been
able to understand it since it does not exist in C and I cant
really visualize the construct in terms of C. That is what my
brain can process. If you understand it so well, you can show
me how one would really implement that kind of construct in
C and then by extension I can see that kind of program flow
in LISP. Whether its imperative programming or functional,
beneath there is program counter and assembly. C is close
to machine so much that it is almost assembly. So understanding try-c-
t in C is equivalent to understanding at
the level of machine language.

I therefore take the liberty to crosspost in C and C++ groups.
C++ compilers that compile to C will produce C code something like this

C++

double safesqrt(double x)
{
if(x < 0)
throw "imaginary root";
return sqrt(x);
}

double foo()
{
if( pow( sqrt(-1), sqrt(-1)) != exp(-M_PI/2) )
printf("Stupid computer can't do basic maths\n");
}

int main(void)
{
try
{
foo();
}
catch(char * err)
{
printf("Sorry %s\n", err);
}
}

C

void * safesqsrt(int *type, double *ret, double x)
{
if(x < 0)
{
*type = CHARSTAR;
return "imaginary root";
}
* ret = sqrt(x);
return 0;
}

void *foo(int *type)
{
double temp;
void *throw;
int throwtype;

throw = safesqrt(&throwtype, &temp, -1.0);
if(throw)
{
*type - throwtype;
return throw;
}
/* etc */
}

int main(void)
{
char *throw;
int throwtype;
char *err;

throw = foo(&throwtype);
if(throw)
goto catch;
return 0;
catch:
switch(throwtype)
{
case CHARSTAR:
err = throw;
printf("Sorry %s\n", err);
break;
}

}

As you see it is totally impractical to try to do this in handwritten C
code for very long, though a compiler will happily chug through it. There
are in fact more subtleties - local objects in foo() and safesqrt() have to
be destroyed.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Oct 21 '07 #5
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>gn*******@gmail.com writes:
>I have some code like this:

(if (test)
(exit)
(do something))
or

(if (test)
( do something)
(exit))

That's Lisp, yes? Saying so would be good, since we naturally assume
that anything posted to comp.lang.c is C.
Actually, most of what is posted here is "not C". Since, according to
many of the regulars, if it includes anything "off topic", it is "not C".

Since all code used in the real world uses extensions, there is no C
code in the real world.

Oct 21 '07 #6
Kenny McCormack wrote:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>>gn*******@gmail.com writes:
>>I have some code like this:

(if (test)
(exit)
(do something))
or

(if (test)
( do something)
(exit))

That's Lisp, yes? Saying so would be good, since we naturally assume
that anything posted to comp.lang.c is C.

Actually, most of what is posted here is "not C". Since, according to
many of the regulars, if it includes anything "off topic", it is "not
C".

Since all code used in the real world uses extensions, there is no C
code in the real world.
Perhaps you mean to say that there are no C _programs_ in the real
world. I'm sure there are pieces of fully Standard C code in many, if
not most programs. It's very likely though that the program, taken as a
whole, includes some non-Standard C.

Oct 21 '07 #7

"Kenny McCormack" <ga*****@xmission.xmission.comwrote in message
>
Since all code used in the real world uses extensions, there is no C
code in the real world.
Sort of true. You'll find a non-trivial program on my website to build fuzzy
logic trees. It is written in pure ANSI C89 except for one detail. The
comma-separated value file loader uses nan to indicate missing values.
Missing values are not allowed in the program, so it plays almost no part in
the main flow control. But it loads, checks for nans, and rejects if they
are present.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Oct 21 '07 #8
[Followup corrected to clc]

santosh said:
Kenny McCormack wrote:
<snip>
>Since all code used in the real world uses extensions, there is no C
code in the real world.

Perhaps you mean to say that there are no C _programs_ in the real
world.
Even if that's what he meant, he's still wrong.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 21 '07 #9
In article <t4******************************@bt.com>,
Malcolm McLean <re*******@btinternet.comwrote:
>
"Kenny McCormack" <ga*****@xmission.xmission.comwrote in message
>>
Since all code used in the real world uses extensions, there is no C
code in the real world.
Sort of true. You'll find a non-trivial program on my website to build fuzzy
logic trees. It is written in pure ANSI C89 except for one detail. The
comma-separated value file loader uses nan to indicate missing values.
Missing values are not allowed in the program, so it plays almost no part in
the main flow control. But it loads, checks for nans, and rejects if they
are present.
Obviously, the statement that "_all_ code used in the real world..."
is false in the mathematical sense of the word "all", but it is true in
the normal sense of the word "all".

But here's the thing: I seriously doubt that, in the hosted world (at
any rate), anything that can be written in "ISO C" (or whatever term you
prefer) should be. I.e., anything that is that pure (such as your fuzzy
logic program) could be written much more easily and readably in
something like AWK.

Oct 21 '07 #10
Kenny McCormack wrote:

<snip>
But here's the thing: I seriously doubt that, in the hosted world (at
any rate), anything that can be written in "ISO C" [ ... ]
Lookup libraries like libTomCrypt, libTomMath etc. Many applications
that do "number crunching", by and large, can be, and often are,
written in Standard C.

Oct 21 '07 #11
On Oct 21, 1:45 am, gnuist...@gmail.com wrote:
I have some code like this:

(if (test)
(exit)
(do something))

or

(if (test)
( do something)
(exit))

Various levels of nestings.

I have several questions, basic to sophisticated.

(1) What is the lisp equivalent idiom for (exit) as in bash or
in C.
(2) What is the best practice to handle this kind of problems?

(3) What is the intermediate practice to handle this kind of
problems.

NOTE: I am really afraid of try-catch-throw. I have never been
able to understand it since it does not exist in C and I cant
really visualize the construct in terms of C. That is what my
brain can process. If you understand it so well, you can show
me how one would really implement that kind of construct in
C and then by extension I can see that kind of program flow
in LISP. Whether its imperative programming or functional,
beneath there is program counter and assembly. C is close
to machine so much that it is almost assembly. So understanding try-c-
t in C is equivalent to understanding at
the level of machine language.

I therefore take the liberty to crosspost in C and C++ groups.
ok i guess we coudl write this one like as below.

if(test)
do something

else
exit
does this solve u r problem..?
If u want to exit from program exit is the keyword..if u want to break
from loops break is the key word.

Oct 21 '07 #12

"Kenny McCormack" <ga*****@xmission.xmission.comwrote in message
>
But here's the thing: I seriously doubt that, in the hosted world (at
any rate), anything that can be written in "ISO C" (or whatever term you
prefer) should be. I.e., anything that is that pure (such as your fuzzy
logic program) could be written much more easily and readably in
something like AWK.
I don't know AWK. If you've got time, try doing it. Seriously. I am not in
the business of selling C compilers, and if fuzzy logic trees are better
implemented in another language I'd be glad to be aware of it.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Oct 21 '07 #13
abhy wrote:

<snip>
If u want to exit from program exit is the keyword..
C has no keyword called exit. exit() is a Standard library function.

Oct 21 '07 #14
In article <Oa******************************@bt.com>,
Malcolm McLean <re*******@btinternet.comwrote:
>
"Kenny McCormack" <ga*****@xmission.xmission.comwrote in message
>>
But here's the thing: I seriously doubt that, in the hosted world (at
any rate), anything that can be written in "ISO C" (or whatever term you
prefer) should be. I.e., anything that is that pure (such as your fuzzy
logic program) could be written much more easily and readably in
something like AWK.
I don't know AWK. If you've got time, try doing it. Seriously. I am not in
the business of selling C compilers, and if fuzzy logic trees are better
implemented in another language I'd be glad to be aware of it.
I may just do that. No promises, but I might get around to it at some
point.

Oct 21 '07 #15
On Sun, 21 Oct 2007 18:39:13 +0100, in comp.lang.c , "Malcolm McLean"
<re*******@btinternet.comwrote:
>
"Kenny McCormack" <ga*****@xmission.xmission.comwrote in message
>>
But here's the thing: I seriously doubt that, in the hosted world (at
any rate), anything that can be written in "ISO C" (or whatever term you
prefer) should be.
Kenny's delusions are hard to understand.
I.e., anything that is that pure (such as your fuzzy
>logic program) could be written much more easily and readably in
something like AWK.
I don't know AWK. If you've got time, try doing it. Seriously.
I _do_ know awk, a little, and its not the beast for the job. awk with
sed, grep, cat and tr, possibly. Yikes.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Oct 21 '07 #16
On Oct 22, 12:38 am, Mark McIntyre <markmcint...@spamcop.netwrote:
On Sun, 21 Oct 2007 18:39:13 +0100, in comp.lang.c , "Malcolm McLean"
<regniz...@btinternet.comwrote:
"Kenny McCormack" <gaze...@xmission.xmission.comwrote in message
But here's the thing: I seriously doubt that, in the hosted world (at
any rate), anything that can be written in "ISO C" (or whatever term you
prefer) should be.
Kenny's delusions are hard to understand.
Yes. It depends on the application domain. In the domains I've
worked in, it's probably true: I need sockets, and generally
threads or a data base. But earlier in my career, I wrote
compilers, and there's nothing in them which can't be readily
expressed in ISO C; this is likely true for any other
application which simply reads input, does some calculations or
transformations, and writes it as output. I've got a lot of
little helper programs which are written in pure ISO C++, and
could almost certainly be written in ISO C with a bit more
effort.
I.e., anything that is that pure (such as your fuzzy
logic program) could be written much more easily and readably in
something like AWK.
I don't know AWK. If you've got time, try doing it. Seriously.
I _do_ know awk, a little, and its not the beast for the job.
awk with sed, grep, cat and tr, possibly. Yikes.
It depends on what the job it, but I agree that I usually end up
using it within a shell script, if only to handle options.

Where AWK really breaks down is when the code gets large enough
that you want to maintain it in separate files. But I use it a
lot for smaller things.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 22 '07 #17
On Sat, 20 Oct 2007 20:45:58 -0000, gn*******@gmail.com wrote:
NOTE: I am really afraid of try-catch-throw. I have never been
able to understand it since it does not exist in C and I cant
really visualize the construct in terms of C.
How try-catch-throw is actually implemented depends on the compiler,
but one can explain it like this.

Assume the following C++ code is written:

#include <cstdio>

// here's a sample object with a constructor and destructor
// to demonstrate scope.
class someobj
{
public:
someobj() { std::puts("constructor"); }
~someobj() { std::puts("destructor"); }
private:
int x; // a dummy member variable
};

// A dummy type, it could be a typedef of int or whatever.
// Just for the purpose of throwing an exception of this particular type.
struct someexceptiontype
{
};

void code_that_may_throw()
{
someobj obj; // instantiating someobj in this scope.

if(false != true)
{
// some error situation happened, throw an exception.
// someexceptiontype() instantiates an object of
// "someexceptiontype" (without binding it into a variable),
// and throw throws it.
throw someexceptiontype();
}

std::puts("wow, false is true");
}

void some_intermediate_function()
{
std::puts("1");
code_that_may_throw();
std::puts("2");
}

int main()
{
try
{
some_intermediate_function();
std::puts("executed without hitch");
}
catch(int e)
{
std::puts("caught an int");
}
catch(someexceptiontype e)
{
std::puts("caught someexceptiontype");
}
std::puts("end of main()");
return 0;
}

The code above contains high-level concepts that approximately translate
to the following lower-level concepts in C. It could be implemented
differently, but the function is the same.

#include <stdio.h>

typedef struct someobj
{
int x;
} someobj;

void someobj__construct(someobj* this)
{
puts("constructor");
if(__system_exception_ptr) goto __scope_end;
__scope_end: ;
}
void someobj__destruct(someobj* this)
{
puts("destructor");
if(__system_exception_ptr) goto __scope_end;
__scope_end: ;
}

struct someexceptiontype
{
};

/*** This global code is defined in some system library by the compiler */
void* __system_exception_ptr = (void*)0;
int __system_exception_type = 0;
void __clear_exception()
{
__system_exception_type = 0;
free(__system_exception_ptr);
__system_exception_ptr = (void*)0;
}
/*** End of compiler library code */

void code_that_may_throw(void)
{
someobj obj; // instantiating someobj in this scope.
someobj__construct(&obj);
if(__system_exception_ptr) goto __scope_end_before_obj;

if(0 != 1)
{
someexceptiontype* e = (someexceptiontype*) malloc(sizeof(*e));
__system_exception_ptr = e;
__system_exception_type = 2;
/* ^ a compiler-specific tag that identifies the exception type */
goto __scope_end;
}

puts("wow, false is true");
if(__system_exception_ptr) goto __scope_end;

__scope_end: ;
someobj__destruct(&obj);
__scope_end_before_obj: ;
}

void some_intermediate_function(void)
{
puts("1");
if(__system_exception_ptr) goto __scope_end;

code_that_may_throw();
if(__system_exception_ptr) goto __scope_end;

puts("2");
if(__system_exception_ptr) goto __scope_end;
__scope_end: ;
}

int main(void)
{
some_intermediate_function();
if(__system_exception_ptr) goto try_catch;
puts("executed without hitch");
if(__system_exception_ptr) goto __scope_end;
goto past_catch;
try_catch: ;
switch(__system_exception_type)
{
case 1: /* example denoting int type */
{
__clear_exception();
puts("caught an int");
if(__system_exception_ptr) goto __scope_end;
break;
}
case 2: /* example denoting someexceptiontype */
{
__clear_exception();
puts("caught someexceptiontype");
if(__system_exception_ptr) goto __scope_end;
break;
}
default:
goto __scope_end; /* still not caught */
}
past_catch: ;
puts("end of main()");
if(__system_exception_ptr) goto __scope_end;

__scope_end: ;
return 0;
}

Of course, for efficiency reasons there is no "if" test after every
function return for exceptions (rather, execution may be transferred
to a dedicated stack and scope unfolder when an exception happens),
but this was the easiest way to explain what happens as regards for
scopes and execution paths.
Also, in the exception handler (catch {}), the exception object is
not supposed to be deallocated until the end of the handler, but
for simplicity I wrote the deallocation first.

Followups set to comp.lang.c++ .

--
Joel Yliluoma - http://bisqwit.iki.fi/
: comprehension = 1 / (2 ^ precision)
Oct 23 '07 #18
On Sat, 20 Oct 2007 20:45:58 -0000, gn*******@gmail.com wrote:
NOTE: I am really afraid of try-catch-throw. I have never been
able to understand it since it does not exist in C and I cant
really visualize the construct in terms of C.
How try-catch-throw is actually implemented depends on the compiler,
but one can explain it like this.

Assume the following C++ code is written:

#include <cstdio>

// here's a sample object with a constructor and destructor
// to demonstrate scope.
class someobj
{
public:
someobj() { std::puts("constructor"); }
~someobj() { std::puts("destructor"); }
private:
int x; // a dummy member variable
};

// A dummy type, it could be a typedef of int or whatever.
// Just for the purpose of throwing an exception of this particular type.
struct someexceptiontype
{
};

void code_that_may_throw()
{
someobj obj; // instantiating someobj in this scope.

if(false != true)
{
// some error situation happened, throw an exception.
// someexceptiontype() instantiates an object of
// "someexceptiontype" (without binding it into a variable),
// and throw throws it.
throw someexceptiontype();
}

std::puts("wow, false is true");
}

void some_intermediate_function()
{
std::puts("1");
code_that_may_throw();
std::puts("2");
}

int main()
{
try
{
some_intermediate_function();
std::puts("executed without hitch");
}
catch(int e)
{
std::puts("caught an int");
}
catch(someexceptiontype e)
{
std::puts("caught someexceptiontype");
}
std::puts("end of main()");
return 0;
}

The code above contains high-level concepts that approximately translate
to the following lower-level concepts in C. It could be implemented
differently, but the function is the same.

#include <stdio.h>

typedef struct someobj
{
int x;
} someobj;

void someobj__construct(someobj* this)
{
puts("constructor");
if(__system_exception_ptr) goto __scope_end;
__scope_end: ;
}
void someobj__destruct(someobj* this)
{
puts("destructor");
if(__system_exception_ptr) goto __scope_end;
__scope_end: ;
}

struct someexceptiontype
{
};

/*** This global code is defined in some system library by the compiler */
void* __system_exception_ptr = (void*)0;
int __system_exception_type = 0;
void __clear_exception()
{
__system_exception_type = 0;
free(__system_exception_ptr);
__system_exception_ptr = (void*)0;
}
/*** End of compiler library code */

void code_that_may_throw(void)
{
someobj obj; // instantiating someobj in this scope.
someobj__construct(&obj);
if(__system_exception_ptr) goto __scope_end_before_obj;

if(0 != 1)
{
someexceptiontype* e = (someexceptiontype*) malloc(sizeof(*e));
__system_exception_ptr = e;
__system_exception_type = 2;
/* ^ a compiler-specific tag that identifies the exception type */
goto __scope_end;
}

puts("wow, false is true");
if(__system_exception_ptr) goto __scope_end;

__scope_end: ;
someobj__destruct(&obj);
__scope_end_before_obj: ;
}

void some_intermediate_function(void)
{
puts("1");
if(__system_exception_ptr) goto __scope_end;

code_that_may_throw();
if(__system_exception_ptr) goto __scope_end;

puts("2");
if(__system_exception_ptr) goto __scope_end;
__scope_end: ;
}

int main(void)
{
some_intermediate_function();
if(__system_exception_ptr) goto try_catch;
puts("executed without hitch");
if(__system_exception_ptr) goto try_catch;
goto past_catch;
try_catch: ;
switch(__system_exception_type)
{
case 1: /* example denoting int type */
{
__clear_exception();
puts("caught an int");
if(__system_exception_ptr) goto __scope_end;
break;
}
case 2: /* example denoting someexceptiontype */
{
__clear_exception();
puts("caught someexceptiontype");
if(__system_exception_ptr) goto __scope_end;
break;
}
default:
goto __scope_end; /* still not caught */
}
past_catch: ;
puts("end of main()");
if(__system_exception_ptr) goto __scope_end;

__scope_end: ;
return 0;
}

Of course, for efficiency reasons there is no "if" test after every
function return for exceptions (rather, execution may be transferred
to a dedicated stack and scope unfolder when an exception happens),
but this was the easiest way to explain what happens as regards for
scopes and execution paths.
Also, in the exception handler (catch {}), the exception object is
not supposed to be deallocated until the end of the handler, but
for simplicity I wrote the deallocation first.

Followups set to comp.lang.c++ .

--
Joel Yliluoma - http://bisqwit.iki.fi/
: comprehension = 1 / (2 ^ precision)
Oct 23 '07 #19
NOTE: I am really afraid of try-catch-throw. I have never been
able to understand it since it does not exist in C and I can't
really visualize the construct in terms of C. That is what my
Actually, these constructs pretty much exist in C as well: `catch' is called
`setjmp', and `throw' is called `longjmp'.
Stefan
Oct 23 '07 #20
On Oct 23, 9:33 am, Stefan Monnier <monn...@iro.umontreal.cawrote:
NOTE: I am really afraid of try-catch-throw. I have never been
able to understand it since it does not exist in C and I can't
really visualize the construct in terms of C. That is what my

Actually, these constructs pretty much exist in C as well: `catch' is called
`setjmp', and `throw' is called `longjmp'.

Stefan
Is it in some obscure corner of K&R ANSI ? I was never taught this one
by my instructor. Can you explain its syntax and patterns of usage ?

Oct 23 '07 #21
Stefan Monnier wrote:
>NOTE: I am really afraid of try-catch-throw. I have never been
able to understand it since it does not exist in C and I can't
really visualize the construct in terms of C. That is what my

Actually, these constructs pretty much exist in C as well: `catch' is
called `setjmp', and `throw' is called `longjmp'.
I believe a better way would be to imagine that 'try', not 'catch',
is called 'setjmp'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 23 '07 #22
On Oct 23, 9:33 am, Stefan Monnier <monn...@iro.umontreal.cawrote:
NOTE: I am really afraid of try-catch-throw. I have never been
able to understand it since it does not exist in C and I can't
really visualize the construct in terms of C. That is what my

Actually, these constructs pretty much exist in C as well: `catch' is called
`setjmp', and `throw' is called `longjmp'.

Stefan
Stefan, let me thank you for what seems to me to be the correct
concept.
I searched this whole thread in google for setjmp and YOU are the only
one who mentioned it. I applaud you. Because, it does not seem that
there
is any other construct that can implement try-catch-throw. I still
have to
read up on it, but thats what my gut instinct says.

Anyone, care to show how this translates into assembly after we deal
thoroughly with this in the context of C ?

Everyone, please ignore the the mean spirits trying to derail a
serious
conceptual discussions and calling each other trolls or giving
obfuscated
explanations for ego purposes, and not LUCID explanation.
Oct 23 '07 #23
On Oct 20, 3:55 pm, "Alf P. Steinbach" <al...@start.nowrote:
* gnuist...@gmail.com:
I have some code like this:
(if (test)
(exit)
(do something))
or
(if (test)
( do something)
(exit))
Various levels of nestings.
I have several questions, basic to sophisticated.
(1) What is the lisp equivalent idiom for (exit) as in bash or
in C.

C++ does not have a built-in 'exit' command. There is a library
function 'exit' which exits the process. One must assume that's not
what you mean, and that you're not asking C and C++ programmers to teach
you Lisp.

Therefore, assuming you want to exit the function or the block.
(2) What is the best practice to handle this kind of problems?

It's not a general class of problem.

Appropriate solutions depend on the problem at hand.

E.g., in C++,

// (if (test) (exit) (do something))

void foo()
{
if( !test )
{
doSomething();
}
}

void bar()
{
if( test ) { return; }
doSomething();
}
(3) What is the intermediate practice to handle this kind of
problems.

?
NOTE: I am really afraid of try-catch-throw. I have never been
able to understand it since it does not exist in C and I cant
really visualize the construct in terms of C. That is what my
brain can process. If you understand it so well, you can show
me how one would really implement that kind of construct in
C and then by extension I can see that kind of program flow
in LISP. Whether its imperative programming or functional,
beneath there is program counter and assembly. C is close
to machine so much that it is almost assembly. So understanding try-c-
t in C is equivalent to understanding at
the level of machine language.

The closest equivalent in C would be a 'longjmp'. However, a C++
exception is more limited, in that it will only jump up the call chain,
and it's more powerful, in that it will destroy local objects as it does
so. Also, if you use 'longjmp' in C++ you're practically doomed (unless
you use it to jump between co-routines with their own stacks), because
'longjmp' doesn't destroy local objects.
Sure you have good ideas.

I still would like an equivalent implementation explained. Ofcourse,
smart
companies and smart programmers were doing all this before C++ came
and even in LISP they have atleast two of try catch throw.

Oct 23 '07 #24
On Tue, 23 Oct 2007 12:33:17 -0400, Stefan Monnier wrote:
>NOTE: I am really afraid of try-catch-throw. I have never been
able to understand it since it does not exist in C and I can't
really visualize the construct in terms of C. That is what my

Actually, these constructs pretty much exist in C as well:
`catch' is called `setjmp', and `throw' is called `longjmp'.
If you ignore the thing about scope that I was being very careful
to illustrate properly, then yes.
And, the fact that try-catch blocks can be nested, recursed, etc,
and only catching the matching type of exception stops the unwinding.

--
Joel Yliluoma - http://bisqwit.iki.fi/
: comprehension = 1 / (2 ^ precision)
Oct 24 '07 #25
>>NOTE: I am really afraid of try-catch-throw. I have never been
>>able to understand it since it does not exist in C and I can't
really visualize the construct in terms of C. That is what my

Actually, these constructs pretty much exist in C as well: `catch' is
called `setjmp', and `throw' is called `longjmp'.
I believe a better way would be to imagine that 'try', not 'catch',
is called 'setjmp'.
Sorry, I'm reading this on gnu.emacs.help where Elisp only provides `catch'
and `throw' (no `try') and these map pretty closely to setjmp/longjmp.
Stefan
Oct 24 '07 #26
Anyone, care to show how this translates into assembly after we deal
thoroughly with this in the context of C ?
I believe that one way to look at setjmp/longjmp in C is that setjmp saves
a copy of the registers (most importantly PC and SP) and longjmp uses that
copy to jump back to the corresponding point in the program (and stack
activation).
Stefan
Oct 24 '07 #27
On Sun, 21 Oct 2007 00:55:53 +0200, "Alf P. Steinbach"
<al***@start.nowrote:
* gn*******@gmail.com:
NOTE: I am really afraid of try-catch-throw. I have never been
able to understand it since it does not exist in C and I cant
really visualize the construct in terms of C. <snip>

The closest equivalent in C would be a 'longjmp'. However, a C++
exception is more limited, in that it will only jump up the call chain,
C longjmp/setjmp also is only guaranteed to work up the stack; the
fact that _some_ implementations can work cross-stack and in
particular cross-thread is not standard nor portable.
and it's more powerful, in that it will destroy local objects as it does
so. Also, if you use 'longjmp' in C++ you're practically doomed (unless
you use it to jump between co-routines with their own stacks), because
'longjmp' doesn't destroy local objects.
Actually it's Undefined Behavior; a good quality C++ implementation
CAN coordinate longjmp, and also pthreads cancellation, with
exceptions to destruct locals cleanly -- but it's not required.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Nov 4 '07 #28
On Mon, 05 Nov 2007 06:07:25 +0100, "Alf P. Steinbach"
<al***@start.nowrote:
* David Thompson:
On Sun, 21 Oct 2007 00:55:53 +0200, "Alf P. Steinbach"
<al***@start.nowrote:
The closest equivalent in C would be a 'longjmp'. However, a C++
exception is more limited, in that it will only jump up the call chain,
C longjmp/setjmp also is only guaranteed to work up the stack; the
fact that _some_ implementations can work cross-stack and in
particular cross-thread is not standard nor portable.

So?

But also, what on Earth do you mean by a cross-thread longjmp? I
implemented coroutines in terms of longjmp at the time that was popular,
so the concepts involved are not unfamiliar to me. Yet I fail to
envision what you could be talking about, especially as "fact". I think
IME 'coroutine' has been used for several slightly different concepts,
but if you mean the one of separate threads of control passing CPU
ownership often along with data anytime they choose, also known more
specifically as cooperative/nonpreemptive threading/tasking, yes. I
think you are agreeing that it did actually work, because 'restoring'
PC and SP (or equivalents) was enough; but I am pointing out it wasn't
and isn't _required_ to work that way.
perhaps you're talking about restoring the full context (registers etc)
of a moment in a thread's execution?
IME a cooperative switch itself doesn't need to save and restore other
state, as the language mechanism(s) e.g. 'call yield' handle it. Or
for cache-y things it happens automatically, or mostly automatically.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Dec 2 '07 #29

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: deko | last post by:
Is there a shorthand way to Exit Sub On Error? This does not seem to work: On Error Exit Sub And I don't want to use: On Error GoTo 0 Must I use: GoTo Exit_Here?
7
by: deko | last post by:
I have a function with a number of long loops. While the function is running, I want to be able to click a Stop button and exit the function as quickly as possible. The abbreviated code looks...
10
by: lallous | last post by:
Hello, This question was asked in comp.lang.c++ and the answers involved the use of objects whose destructors are automatically called when getting out of scope, however I was expecting...
17
by: jwaixs | last post by:
Hello, I was wondering, what's the difference between exit and return in the main() function? For me they both look the same, or aren't they? And if they aren't, which should I use in which...
6
by: orekin | last post by:
Hi There I have been trying to come to grips with Application.Run(), Application.Exit() and the Message Pump and I would really appreciate some feedback on the following questions .. There are...
3
by: darrel | last post by:
This might be a really dumb question, but when should/shouldn't one use the exit function command? If I have this: function() if something then do this return that else
2
by: Serious_Practitioner | last post by:
Good day, and thank you in advance for any assistance. I'm having trouble with something that I'm trying for the first time. Using Access 2000 - I want to run a function either on the click of a...
23
by: Tina | last post by:
In vb.net to get out of a sub or a function we can say Exit Sub. How can I get out of a void method in C#. I can't find that documented anywhere. Thanks, T
14
by: tshad | last post by:
Is there any difference between Return and Exit Sub? I have some code that uses both when I have an error in my Sub. Thanks, Tom
11
by: yawnmoth | last post by:
To quote from <http://php.net/function.include>, "Because include() is a special language construct, parentheses are not needed around its argument. Take care when comparing return value." ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.