OMG a "Goto"
-
protected void Page_Load(object sender, EventArgs e)
{
string str = "Even exception occur again in catch block still finally block is Working";
try
{
throw new IndexOutOfRangeException();
}
catch
{
goto Hello;
throw new IndexOutOfRangeException();}
finally
{
Response.Redirect("Error.aspx?str="+ str);
}
Hello:
Response.Write("Finally Skiped!");
}http://www.codeproject.com/KB/aspnet/DotNetBulletQuestions.aspx?msg=2764693#xx2764693xx[^] A goto in a try/catch/finally block... urrrghh!
:wtf:
"The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
-
Maybe someone was just trying to test something and forgot to yank out the code? One can dream right....
(You need to follow the link.)
-
(You need to follow the link.)
Gotcha. ;D
-
Rob Grainger wrote:
not used as an alternative control flow mechanism.
That's exactly what they are (IMHO). :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]CPallini wrote:
That's exactly what they are (IMHO).
Guess so, but the operative bit to my eyes is "alternative". Exceptions are much heavier weight than loop variables etc. in terms of the execution model. Enclosing code in try/catch involves manipulation of the stack frame, tracking exception-handling blocks and other devilry. Personally, if a condition can be anticipated, I consider it better to manage using traditional control flow mechanisms.
-
What I mean is: a 'break' would be enough to execute the 'beep bop a loola' :-D ... By the way: I'd consider a 'flag' much better than a 'goto'... Regards Thomas
Thomas Weller wrote:
By the way: I'd consider a 'flag' much better than a 'goto'...
Personally, I don't like introducing variables to track these kinds of conditions, but I guess at these levels it really comes down to choice of programming style. Granted, though, that often the need to perform such trickery may often signal a deficiency that may better be addressed by refactoring.
-
CPallini wrote:
That's exactly what they are (IMHO).
Guess so, but the operative bit to my eyes is "alternative". Exceptions are much heavier weight than loop variables etc. in terms of the execution model. Enclosing code in try/catch involves manipulation of the stack frame, tracking exception-handling blocks and other devilry. Personally, if a condition can be anticipated, I consider it better to manage using traditional control flow mechanisms.
You're right and personally I won't use exceptions for that piece of code (I just made a not about the possibility). BTW I won't even write such a piece of code. :rolleyes:
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Thomas Weller wrote:
By the way: I'd consider a 'flag' much better than a 'goto'...
Personally, I don't like introducing variables to track these kinds of conditions, but I guess at these levels it really comes down to choice of programming style. Granted, though, that often the need to perform such trickery may often signal a deficiency that may better be addressed by refactoring.
Rob Grainger wrote:
often the need to perform such trickery may often signal a deficiency that may better be addressed by refactoring
I fully agree on that. The need to introduce variables (or a 'goto') for the mere sake of controlling execution flow to me is clearly what is called a 'code smell'.
Rob Grainger wrote:
I guess at these levels it really comes down to choice of programming style
Yup. The one way isn't really much better than the other... Regards Thomas
-
I like this reason as well void foobar() { // blah blah blah for(...) { // blablabla for(..) { // foo foo foo if(bar) goto end; } } end: // beep bop a loola }
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
Even Java has the "goto" similarity where you can "continue" to a label.
-
I like this reason as well void foobar() { // blah blah blah for(...) { // blablabla for(..) { // foo foo foo if(bar) goto end; } } end: // beep bop a loola }
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
-
protected void Page_Load(object sender, EventArgs e)
{
string str = "Even exception occur again in catch block still finally block is Working";
try
{
throw new IndexOutOfRangeException();
}
catch
{
goto Hello;
throw new IndexOutOfRangeException();}
finally
{
Response.Redirect("Error.aspx?str="+ str);
}
Hello:
Response.Write("Finally Skiped!");
}http://www.codeproject.com/KB/aspnet/DotNetBulletQuestions.aspx?msg=2764693#xx2764693xx[^] A goto in a try/catch/finally block... urrrghh!
I've programmed for over 10 years. The old BASIC, like GW-Basic or BasicA, yes I can honestly say that goto came in handy. However, I never once used it in C,C++, VB.NET or C#. Never. Since all modern languages have a Continue statement and a Break statement (for loops), I never needed a use for a goto. The reason, I'm guessing, that its not being removed in modern languages is to keep it compatible with old code. Very rarely does a language "lose" a command. They keep them so old code is compatible.
-
try it! you will improve you C# knowledge! or maybe read better... it's a double nested loop
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
-
I've programmed for over 10 years. The old BASIC, like GW-Basic or BasicA, yes I can honestly say that goto came in handy. However, I never once used it in C,C++, VB.NET or C#. Never. Since all modern languages have a Continue statement and a Break statement (for loops), I never needed a use for a goto. The reason, I'm guessing, that its not being removed in modern languages is to keep it compatible with old code. Very rarely does a language "lose" a command. They keep them so old code is compatible.
It's an understatement to say that "goto came in handy" in the old style BASIC. You had to use
GOTO
because the language was severely lacking in control flow.DO
...LOOP
didn't exist, so you had to make those loops withGOTO
.SELECT CASE
didn't exist, so you had to useON
...GOTO
. Multi-lineIF
statements didn't exist, so you either had to cram everything on one line or useGOTO
. And exceptions didn't exist, so you usedON ERROR GOTO
. So you ended up with code like this, filled withGOTO
. And furthermore, line numbers were mandatory on every line, so it was very difficult to tell which lines wereGOTO
targets and which weren't. And this is what caused all the animosity towards theGOTO
statement. -
protected void Page_Load(object sender, EventArgs e)
{
string str = "Even exception occur again in catch block still finally block is Working";
try
{
throw new IndexOutOfRangeException();
}
catch
{
goto Hello;
throw new IndexOutOfRangeException();}
finally
{
Response.Redirect("Error.aspx?str="+ str);
}
Hello:
Response.Write("Finally Skiped!");
}http://www.codeproject.com/KB/aspnet/DotNetBulletQuestions.aspx?msg=2764693#xx2764693xx[^] A goto in a try/catch/finally block... urrrghh!
LMAO!!!! Didn't release it even existed in c#!! Love it! That will show those annoying OO purest with their fancy classes and methods and thingies!! :laugh: Go the GOTO!
-
protected void Page_Load(object sender, EventArgs e)
{
string str = "Even exception occur again in catch block still finally block is Working";
try
{
throw new IndexOutOfRangeException();
}
catch
{
goto Hello;
throw new IndexOutOfRangeException();}
finally
{
Response.Redirect("Error.aspx?str="+ str);
}
Hello:
Response.Write("Finally Skiped!");
}http://www.codeproject.com/KB/aspnet/DotNetBulletQuestions.aspx?msg=2764693#xx2764693xx[^] A goto in a try/catch/finally block... urrrghh!
-
in C++ drop throw switch-statement is allowed in C# not - if it was that, you didn't see.
-
You may throw an exception for that. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]hmm, what do you think? Why is the name of the Exception object "Exception" - because you should (ab)use it for control program flow? So I see it like this: Use Exceptions for exceptions Avoid goto, but if you must, why not use goto? A single goto, for a purpose as shown above, for shure does not leed to spaghetti-code, but don't try to replace all your for/whiles... with a goto. ;P
-
I know. But in the pseudocode above it will definitely execute the 'beep bop a loola'... :-D Regards Thomas
wrong! read the code!
-
protected void Page_Load(object sender, EventArgs e)
{
string str = "Even exception occur again in catch block still finally block is Working";
try
{
throw new IndexOutOfRangeException();
}
catch
{
goto Hello;
throw new IndexOutOfRangeException();}
finally
{
Response.Redirect("Error.aspx?str="+ str);
}
Hello:
Response.Write("Finally Skiped!");
}http://www.codeproject.com/KB/aspnet/DotNetBulletQuestions.aspx?msg=2764693#xx2764693xx[^] A goto in a try/catch/finally block... urrrghh!