GoTo


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: GoTo

  1. #1
    Join Date
    Oct 2004
    Posts
    151

    Question GoTo

    Hi everyone,

    I need to use something in java that is similar to the C language goto statement.

    This is what i have in a function

    Code:
    public int ret()
    {
    
    if
    {
    //something
    //something
    
    //This is the part where i need to jump to the end of the
    //function and not exit the function
    }
    
    //This is the part i need to jump to
    
    //some more code
    //some more code
    }
    Is there a way in which the above problem can be got around. Usually in the C language i would use the goto statement. I can't use a labeled break here as there is no loop in my program.

    I hope someone can help me this problem

    Thank You

    Yours Sincerely

    Richard West

  2. #2
    Join Date
    May 2004
    Location
    Durham, UK
    Posts
    174
    Hi,Richard

    use try/throw and catch

    i.e.

    Code:
    public int ret()
    {
    
    try{
    
    if
    {
    //something
    //something
    
    throw new Exception();
    //This is the part where i need to jump to the end of the
    //function and not exit the function
    }
    
    }catch(Exception e)
    {
    //This is the part i need to jump to
    
    //some more code
    //some more code
    }
    }
    see http://java.sun.com/docs/books/tutor...al/exceptions/
    for more info.
    Hope this helps
    Graham

    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, and if they get mad, you are a mile away and you have their shoes ;-)

    http://www.grahamrobinsonsoftware.com

  3. #3
    Join Date
    Jun 2004
    Posts
    10
    1. Do not try to use a construct similar to a goto statement. Otherwise, the result is code that is more difficult to maintain.

    2. Don't throw exceptions unless the situation is truly an exception.

  4. #4
    Join Date
    May 2004
    Location
    Durham, UK
    Posts
    174
    Chimaera,

    I think this is fair enough, although as neither of us know exactly what Richard is using it for, all we can do is provide the tools.....

    Cheers
    Graham

  5. #5
    Join Date
    Dec 2004
    Location
    Norway
    Posts
    14
    Originally posted by chimaera
    2. Don't throw exceptions unless the situation is truly an exception. [/B]
    I disagree, if you want to use the benefit of having error-
    conditions bubble up through your method call hierachy
    and treat them consistently in one place, there is no better way than using an exception.

    No matter what professors may preach.
    Java Wreck

  6. #6
    Join Date
    Jun 2004
    Posts
    10
    Originally posted by sjalle
    I disagree, if you want to use the benefit of having error-
    conditions bubble up through your method call hierachy
    and treat them consistently in one place, there is no better way than using an exception.

    No matter what professors may preach.

    You are right about "bubbling up" exceptions, but don't get the terms error and exception confused. Refer to http://java.sun.com/docs/books/tutor...efinition.html for a good explanation about exceptions.

  7. #7
    Join Date
    Dec 2004
    Location
    Norway
    Posts
    14
    No...I try not to, but an exception is an error of some sort,
    but an error doesn't have to be treated as an exception.
    In my opinion it depends on the complexity of the code and what
    option is the easiest to maintain through the lifecycle of the application.

    In other words, defining what is an error and what is an
    exception may be a cosy academic pasttime, but its like the river,
    you cross it when you get there, on the rivers terms.

    Exception caused by bad program logics should never be caught programmatically except during development & testing
    Java Wreck

  8. #8
    Join Date
    Jun 2004
    Posts
    10
    Originally posted by sjalle
    No...I try not to, but an exception is an error of some sort,
    but an error doesn't have to be treated as an exception.
    In my opinion it depends on the complexity of the code and what
    option is the easiest to maintain through the lifecycle of the application.

    In other words, defining what is an error and what is an
    exception may be a cosy academic pasttime, but its like the river,
    you cross it when you get there, on the rivers terms.

    Exception caused by bad program logics should never be caught programmatically except during development & testing
    Actually, an Error has been defined. http://java.sun.com/j2se/1.5.0/docs/...ang/Error.html

    I understand and agree mostly to what you are saying, but in the case above, throw and catching exceptions for the purposes of branching is certainly not the "easiest to maintain through the lifecycle of the application."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center