[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to exit out of a function ? what is try-catch-throw in terms of
From: |
Jim Langston |
Subject: |
Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter |
Date: |
Sat, 20 Oct 2007 18:08:11 -0700 |
<gnuist006@gmail.com> wrote in message
1192913158.922454.108100@k35g2000prh.googlegroups.com">news:1192913158.922454.108100@k35g2000prh.googlegroups.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";
}
}
- Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter, (continued)
- Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter, Keith Thompson, 2007/10/21
- Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter, Kenny McCormack, 2007/10/21
- Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter, Malcolm McLean, 2007/10/21
- Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter, Kenny McCormack, 2007/10/21
- Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter, santosh, 2007/10/21
- Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter, Malcolm McLean, 2007/10/21
- Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter, Kenny McCormack, 2007/10/21
- Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter, santosh, 2007/10/21
- Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter, Richard Heathfield, 2007/10/21
- Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter, Mark McIntyre, 2007/10/22
Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter,
Jim Langston <=
Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter, Malcolm McLean, 2007/10/21
Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter, abhy, 2007/10/21
Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter, Joel Yliluoma, 2007/10/23
Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter, Stefan Monnier, 2007/10/23