-
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
-
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
-
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.
-
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
-
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
-
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.
-
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
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks