PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > How to exit out of a function ? what is try-catch-throw in terms of Program Counter
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How to exit out of a function ? what is try-catch-throw in terms of Program Counter

Réponse
 
LinkBack Outils de la discussion
Vieux 20/10/2007, 21h45   #1 (permalink)
gnuist006@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.

  Réponse avec citation
Vieux 20/10/2007, 21h57   #2 (permalink)
gnuist006@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter

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 ?


  Réponse avec citation
Vieux 20/10/2007, 23h55   #3 (permalink)
Alf P. Steinbach
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to exit out of a function ? what is try-catch-throw in termsof Program Counter

* gnuist006@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.



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


Uh.

Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  Réponse avec citation
Vieux 21/10/2007, 02h08   #4 (permalink)
Jim Langston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter

<gnuist006@gmail.com> wrote in message
news:1192913158.922454.108100@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";
}
}


  Réponse avec citation
Vieux 21/10/2007, 03h30   #5 (permalink)
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter

gnuist006@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) kst-u@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"
  Réponse avec citation
Vieux 21/10/2007, 09h03   #6 (permalink)
Malcolm McLean
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter


<gnuist006@gmail.com> wrote in message
news:1192913158.922454.108100@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


  Réponse avec citation
Vieux 21/10/2007, 11h44   #7 (permalink)
Kenny McCormack
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter

In article <lnodetyzbc.fsf@nuthaus.mib.org>,
Keith Thompson <kst-u@mib.org> wrote:
>gnuist006@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.

  Réponse avec citation
Vieux 21/10/2007, 11h55   #8 (permalink)
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter

Kenny McCormack wrote:

> In article <lnodetyzbc.fsf@nuthaus.mib.org>,
> Keith Thompson <kst-u@mib.org> wrote:
>>gnuist006@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.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 01h52.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17215 seconds with 16 queries