how to emulate goto.

L

Larry Barowski

Gaurav Agarwal said:
Hi,
In a method I intend to do the following:

void foo() {

do_processing;
if (error)
goto exit;

do_more_processing;
if (error)
goto exit;
.
.
.

exit:
return some value;
}

If you need to do cleanup code at the end, a
try/finally (without a catch) is the obvious
structure:

void foo () {
try {
... do_processing
if (error)
return;
... do_more_processing
if (error)
return;
...
}
finally {
... cleanup
}
}
 
G

Gaurav Agarwal

Hi,
In a method I intend to do the following:

void foo() {

do_processing;
if (error)
goto exit;

do_more_processing;
if (error)
goto exit;
..
..
..

exit:
return some value;
}

Since I cannot use goto in java I cannot jump to the end of a function.
Also break can only take me to the start of a loop and not to the end
of the loop.
If possible I would not want to embed "return false;" everytime I check
for error.

Any advice?

Gaurav
 
T

Thomas Weidenfeller

Gaurav said:
Hi,
In a method I intend to do the following:

void foo() {

do_processing;
if (error)
goto exit;

do_more_processing;
if (error)
goto exit;

As people have explained to you in great length, you don't have a goto
in Java. Use on of the other control statements as has been suggested to
you. Posting the same question again and again will not get you a
different answer.

/Thomas
 
J

Jacob

Gaurav said:
Hi,
In a method I intend to do the following:

void foo() {

do_processing;
if (error)
goto exit;

do_more_processing;
if (error)
goto exit;
.
.
.

exit:
return some value;
}


void foo()
{
doProcessing();

if (!isError) doMoreProcessing();

if (!isError) do...

:

wrap up
}
 
M

Matt Humphrey

Gaurav Agarwal said:
Hi,
In a method I intend to do the following:

void foo() {

do_processing;
if (error)
goto exit;

do_more_processing;
if (error)
goto exit;
.
.
.

exit:
return some value;
}

What you want is:

sonevaluetype foo () {

try {
do_processing ();
if (error) throw new Exception ("problem");

do_more_processing ();
if (error) throw new Exception ("another problem");

} catch (Exception ex) {
// Handle your problem here, but
// do not return or rethrow the exception
}

return some value;
}
Since I cannot use goto in java I cannot jump to the end of a function.

Faulty logic.
Also break can only take me to the start of a loop and not to the end
of the loop.
If possible I would not want to embed "return false;" everytime I check
for error.

If the processing of your method must stop because of an error, there is no
point in trying to maintain the flow of the rest of the algorithm.
Structure the code to match the problem--there is nothing wrong with simply
returning false (or throwing an exception, etc) Read up on the
try-catch-finally statement.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
G

Gaurav Agarwal

I didn't know that I had posted the same question before. Can you point
me to the post. I also did not know that people had explained this to
me in great lengths. Do you know something that I don't.
As far as I know this is my second post on this group and the first one
dealt with session IDs.
 
S

Skip

Also break can only take me to the start of a loop and not to the end
of the loop.

[break] takes you to the end of the loop.
[continue] takes you to the begin of the loop.
[return] bring you to the end of the method.
If possible I would not want to embed "return false;" everytime I check
for error.

Why not, that's the best solution.
 
D

David Van de Voorde

As a reaction to the previous poster:
In general, exception handling should not be used as a replacement for
program logic (i.e. loops, if statements, ...). However, the solution looks
nice.


As an answer to the original question:

See http://java.sun.com/developer/TechTips/2000/tt0613.html

-> look for "GOTO STATEMENTS AND JAVA PROGRAMMING"

Excerpt:

static int add(int a, int b) {
block1: {
if (a < 0) {
break block1;
}
if (b < 0) {
break block1;
}
return a + b;
}
throw new IllegalArgumentException(
"a < 0 || b < 0");
}

The stmnt "break block1;" means "goto the 1st statement AFTER the statement
with label block1"

Personally, I think that everybody has his own way to hanlde constructs like
this. And to me it seems that it can never be solved cleanly. You should
take the approach that for you seems to entangle the least with the "normal"
process flow; i.e. the approach that divides as much as possible the
"exception cases" from the "normal cases".

David.
 
T

Thomas Schodt

Gaurav said:
I didn't know that I had posted the same question before.
Can you point me to the post.

It is possible you were mistaken for someone else,
here's a candidate
<http://forum.java.sun.com/thread.jspa?threadID=591511>

Or Mr. Weidenfeller could be addressing everyone who asks this question,
like a teacher addresses all the students with "you" (plural).
I also did not know that people had explained this to
me in great lengths.

Google?
It certainly *has* been explained in great detail countless times.
You can probably buy one-on-one tutoring somewhere
if you think you need to have it explained to *you* (singular).
Do you know something that I don't.

The smart money is on "yes".
<http://www.google.co.uk/groups?as_ugroup=comp.lang.java.*&as_uauthors=thomas weidenfeller>
 
B

Birkemose

...
It is possible you were mistaken for someone else,
here's a candidate
<http://forum.java.sun.com/thread.jspa?threadID=591511>

Or Mr. Weidenfeller could be addressing everyone who asks this question,
like a teacher addresses all the students with "you" (plural).

Maybe the OP simply was not aware that Mr. Weidenfeller apparently is a
teacher...
Or maybe Mr. Weidenfeller should learn to express himself in a civilized
manner ( not consistent with the teacher fact ;)
Google?
It certainly *has* been explained in great detail countless times.
You can probably buy one-on-one tutoring somewhere
if you think you need to have it explained to *you* (singular).

Even if I admit Google knows most things, the purpose of a newsgroup like
this is to talk about java programming.
That is what the OP did, and just because he failed to find the answer
elsewhere, doesnt mean he should be treated bad !

I really fail to see how the fact that Mr. Weidenfeller posts a lot of
messages, has anything to do with how smart he is, for all it matters, most
of his posts seem to underline how stupid the OP is, but has little
contents.
Dont mistake newbies for stupid people, like some of the regular posters
inhere seem to do..


Lars
 
S

Steve Horsley

Gaurav said:
I didn't know that I had posted the same question before. Can you point
me to the post. I also did not know that people had explained this to
me in great lengths. Do you know something that I don't.
As far as I know this is my second post on this group and the first one
dealt with session IDs.

Actually, I thought you had as well, but checking back it seems that I am
confusing you with someone else too. The question was asked only a day or two
ago, with almost identical wording. It is a common question, and a google
search before you post would probably have got your answer quicker.

Steve
 
P

Patricia Shanahan

Gaurav said:
Hi,
In a method I intend to do the following:

void foo() {

do_processing;
if (error)
goto exit;

do_more_processing;
if (error)
goto exit;
..
..
..

exit:
return some value;
}

Since I cannot use goto in java I cannot jump to the end of a function.
Also break can only take me to the start of a loop and not to the end
of the loop.
If possible I would not want to embed "return false;" everytime I check
for error.

Any advice?

Gaurav

Note that "return false;" only applies to boolean methods,
not void methods like foo. It would just be "return".

In general, it makes more sense than you would think to
embed returns at give-up points in a Java method. The best
reason for NOT doing it is the risk that at some point you
will need to have some clean-up code that gets run on every
completion of the method. In Java, you can use try-finally
to force the clean-up code to run.

If the failure is something foo's caller needs to deal with,
consider throwing an exception instead of returning.

Patricia
 
A

Andrew McDonagh

Gaurav said:
Hi,
In a method I intend to do the following:

void foo() {

do_processing;
if (error)
goto exit;

do_more_processing;
if (error)
goto exit;
..
..
..

exit:
return some value;
}

Since I cannot use goto in java I cannot jump to the end of a function.
Also break can only take me to the start of a loop and not to the end
of the loop.
If possible I would not want to embed "return false;" everytime I check
for error.

Any advice?

Gaurav

Frankly, let me be a bit more blunt than most of the (good) replies you
have had showing various means of programming without gotos....

You don't want to emulate gotos, You dont want to use gotos even in
languages that do support them.

Its been known for a very long time, that the goto is a crap mechanism,
used usually at the wrong time, by the less experienced developers.

If you were programming in a procedural language then I could just about
understand why you may think about using them (regardless of the fact
that I'd still give you the same advice as above), yet you are
developing in an OO language. Gotos have even less right to be used in
a OO language.

A better question you could have asked would be...." I have this code
which uses gotos, how can I rewrite it without gotos?"
 
P

Patricia Shanahan

Andrew McDonagh wrote:
....
You don't want to emulate gotos, You dont want to use
gotos even in languages that do support them.

Its been known for a very long time, that the goto is a
crap mechanism, used usually at the wrong time, by the
less experienced developers.

If you were programming in a procedural language then I
could just about understand why you may think about using
them (regardless of the fact that I'd still give you the
same advice as above), yet you are developing in an OO
language. Gotos have even less right to be used in a OO
language.

A better question you could have asked would be...." I
have this code which uses gotos, how can I rewrite it
without gotos?"

I agree that one should not emulate goto, or even think
about emulating goto. More generally, it is a mistake when
learning language B to think in terms of emulating low level
features of language A. It is much better to think in terms
of how to solve a problem in B.

However, I don't understand the relationship between the
use, or non-use, of goto and OO.

If Java had its entire object structure just the way it is
now, but didn't have try-finally, I would want a way
to implement clean-up code. Conversely, if exception
handling and final clauses were added to C, with no other
changes, I would not have used its goto.

Patricia
 
A

Andrew McDonagh

Patricia said:
Andrew McDonagh wrote:
....


I agree that one should not emulate goto, or even think
about emulating goto. More generally, it is a mistake when
learning language B to think in terms of emulating low level
features of language A. It is much better to think in terms
of how to solve a problem in B.

However, I don't understand the relationship between the
use, or non-use, of goto and OO.

Its just IHO, goto puts too strong an emphasis on procedural code within
the OO classes. This makes the restriction of being able to replace the
conditionality statements with polymorphic calls.

In essence, we'd have procedural function calls, rather than messages
between objects.

If Java had its entire object structure just the way it is
now, but didn't have try-finally, I would want a way
to implement clean-up code.

There's always pure OOA/D/P ways of performing clean-ups, the Java
try-finally merely makes this 'simpler' to implement. Essentially the
try-finally construct could be thought of, as a means of decorating
certain object messaging, with some additional 'clean-up' afterwards.

Don't get me wrong, having this ability built into the language can be a
good thing.... but not always.

Personally I've never yet encountered a situation where I've needed the
try-finally construct. As its always been more appropriate to have
greater control over the 'clean-up'. Sometimes this control has been via
polymorphic messaging too.

Not that using it would worry me, I just haven't required it (yet).

Conversely, if exception
handling and final clauses were added to C, with no other
changes, I would not have used its goto.

Patricia

I can understand your point. However, there are C developers who do not
ever or very rarely use Cs goto. They prefer conditional logic within
the function to handle the situation.

Andrew
 
D

Dimitri Maziuk

Andrew McDonagh sez:
Frankly, let me be a bit more blunt than most of the (good) replies you
have had showing various means of programming without gotos....

You don't want to emulate gotos, You dont want to use gotos even in
languages that do support them.

Its been known for a very long time, that the goto is a crap mechanism,
used usually at the wrong time, by the less experienced developers.

If you were programming in a procedural language then I could just about
understand why you may think about using them (regardless of the fact
that I'd still give you the same advice as above), yet you are
developing in an OO language. Gotos have even less right to be used in
a OO language.

Frankly, you haven't a clue (blunt, eh?). The problem with goto
is that 40 year ago (that's 500BC in computer years) it was the
only means to reuse code, and the resulting code was horrible.
So they invented subroutines and started preaching "goto bad,
subroutines good" mantra.

Today nobody's ever seen a language without subroutines, so the
"subroutines good" part got forgotten and nobody's ever seen the
original spaghetti code, so they don't remember what the "goto
bad" part was all about.

Meanwhile, goto changed its name to "continue break label" and
everyone's using it in the happy ever after.

Dima
 
T

Thomas Schodt

Birkemose said:
...

Maybe the OP simply was not aware that Mr. Weidenfeller apparently
is a teacher...

Huh?
I do not follow your chain of reasoning.

... the purpose of a newsgroup
like this is to talk about java programming.
That is what the OP did, and just because he failed to find the answer
elsewhere, doesn't mean he should be treated bad !

Many regulars here expect posts to clj.programmer to be
not entirely trivial, or at least in scope and well defined.

There's some good reading on this if you follow the links in the FAQ
I really fail to see how the fact that Mr. Weidenfeller posts a lot of
messages, has anything to do with how smart he is,

People will infer what they want from someones posting history.

... most
of his posts seem to underline how stupid the OP is, but has little
contents.

Most? As in the majority of?
I've been around for 6 months and have not noticed anything like that.
After checking his posts for January (the sheer bulk makes anything more
exhausive impractical IMO, but feel free...), I would have to say I do
not see where you get it from.


BTW.
It is probably unlikely that Mr. Weidenfeller will read any of your
posts anytime soon - changing your 'identity', making him add another
entry to his killfile.... tsk tsk....

Dont mistake newbies for stupid people, like some of the regular
posters inhere seem to do..

Many regulars here also answer questions in clj.help where trivial
questions stand a much better chance of getting the kind of answers that
are likely to help newbies.
 
B

Birkemose

...
BTW.
It is probably unlikely that Mr. Weidenfeller will read any of your
posts anytime soon - changing your 'identity', making him add another
entry to his killfile.... tsk tsk....

Hahaha :) Im afraid Mr Weidenfeller can not take credit for that, I have
used both nicks ( and a few others ) for many years now...
Many regulars here also answer questions in clj.help where trivial
questions stand a much better chance of getting the kind of answers that
are likely to help newbies.

Very simple, then guide them to these newsgroups.
 
B

Birkemose

...
Are you volunteering (for even the next 100 or so)?

Haha... nope :)
My original point ( and why I took offense to Mr Weidenfeller ) was, that IF
you answer such questions, do it in a polite way, and do NOT patronize
people just for being newbies...
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top