goto and exception

Hi i have always read that one should avoid goto. But i don't come to know the reason y?
some say its degrade the readability, that's right.

But what i wants to know is that if i use goto in catch section of my code is that valid and do control jump to that code.

void function() {
try {

}
catch(int) {
goto : ERROR;
}
// rest of code here.

ERROR:
//clean up resources
}

In the code above is there any thing wrong? Also where the control goes after executing the code below the ERROR label?

Thanks

You will always get mixed answers for whether to use goto or not. It depends upon your need

In your case, goto statement will work. But you have to make sure there will not be side effect.

On the other sense, catch is normaly used to clean up the resources then why you need a goto statement.

If you are going to use the same code in many places, you can simply put it inside a method and call that function inside catch.
there's nothing wrong with goto itself. But there are many things wrong with it's misuses.
You need to be very careful when you decide to use it.
IMO the code above ( unless possible declaration problems in unshown code ) is quite fine
Last edited on
I don't like it. I would rather see RAII handling the resources for you. The above strikes me as one of those things that can be casually shrugged off but is still more error prone and harder to maintain.
Last edited on
If you can even consider writing code like that, I'd say you can't be trusted with goto and must never use it.
Last edited on
dearvivekkumar wrote:
In the code above is there any thing wrong?
Well, your code is a good example why goto is a no go.

The label 'ERROR' is misleading since that code below will be executed regardless whether an error occured or not. You must keep that in mind.

The code above that label may want to participate in the result of a function below the label (executed independently of the error). This should be no problem, but how will you accomplish that?
@srinathduraisamy
If you are going to use the same code in many places, you can simply put it inside a method and call that function inside catch.


If i use a function then getting the references to the resource will be typical, i think.
i think goto will not serve the purpose in catch{} block. As after executing the code below ERROR label the control never returns back and that function stack gets unwind.

If m wrong, please explain.
here's my example of goto-coding beacuse i hate forloop-coding..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
main()
{ 
     int seconds=60, minutes=5;
     a:;
     sleep(1);
     clrscr();
     seconds=seconds-1;
          printf("%d:%d",minutes,seconds); 
     if (seconds==0)
     {
          if (minutes==0 && seconds==0)
     {
          clrscr();
     printf("Time is Over!");
     exit();
          }
     else
          minutes=minutes-1;
          seconds=seconds+60;
     goto a;
          }
     goto a;
          }


:D. thats how i control my program try it w/o try and catch..
-joenel
Last edited on
closed account (z05DSL3A)
What a pile of crap!
dearvivekkumar wrote:
i think goto will not serve the purpose in catch{} block. As after executing the code below ERROR label the control never returns back and that function stack gets unwind.


That statement doesn't make any sense. Why goto out of the catch? The purpose of the catch is to handle the exception, do cleanup and then rethrow or continue on. The stack doesn't "unwind" in your example. The function would just return which is not the same thing as propagating an exception.

You could easily do a google search on goto and read about the many reasons that people like it or don't like it. The discussion has been had many times but it is obvious that you didn't bother to do any research.
The snippet appears to be an attempt to use two different error handling policies in the same function. It makes me think that the program is not exception safe but happens to use a library or something that commonly throws. The gotos are just silly.
the solution is also goto.. goto bill gates and ask him for the correction of your codes
gotos are silly.
If you are using C++, stick with exceptions.

If you want exceptions in C, that's what the <setjmp.h> library is for.
http://www.cplusplus.com/reference/clibrary/csetjmp/

Examples
http://www.google.com/search?q=exceptions+in+C&search=

A really nice implementation
http://cexcept.sourceforge.net/

There are important differences between C and C++ exception handling, particularly with how resources are cleaned up. Make sure to implement some proper RAII.

Also keep in mind that C is not designed to work around an exception model, so design your algorithms appropriately.
Topic archived. No new replies allowed.