aspose file tools*
The moose likes Java in General and the fly likes can we send the control from finally to try? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "can we send the control from finally to try?" Watch "can we send the control from finally to try?" New topic
Author

can we send the control from finally to try?

Gaurav Chikara
Ranch Hand

Joined: Jun 09, 2000
Posts: 410
Suppose I get an exception
in my case i m getting ArrayindexOutofBounds Exception
in which i m resetting the variable back to 0 (it had some value which was causing this exception to happen)
now i want the control to be sent back to try block
through finally block
Is there any crack or way out for this to happen
Im very confused Pls suggest


SCJP,SCWCD,SCBCD<br />If Opportunity doesn't knock then build the door
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Hi Gaurav,
I have to suggest that if you are getting an ArrayIndexOutOfBounds exception, you need to take another look at your code. It should be very straightforward to avoid this type of exception, rather than trying to set your index back to 0 and change the flow of control.
I'm sure that if you post an example of the code that is giving you this problem, we can come up with another way to achieve this.
Stephanie
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
You can put the try/catch/finnaly within a loop and just set a flag that will let the try block continue in either your catch or finally block.
Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Gaurav Chikara
Ranch Hand

Joined: Jun 09, 2000
Posts: 410
As per said by Bosun
does it mean that i have to wrote the whole try code in finally or catch block?
As per said by Stephanie
My code stores the values in session object and i keep an index of the duplicate value stored in a vector
and when control passes to next program which generates the xml
based on this index i remove duplicate values from the same vector in that program but my first program index value remains same and now when i again try to add element as fresh
that index value throws ArrayIndexOutOfBoundsException thats
why I have to reset it to 0 in catch block and then only in next attempt program runs fine
I hope I have explained my problem clearly as per demanded

Originally posted by Bosun Bello:
You can put the try/catch/finnaly within a loop and just set a flag that will let the try block continue in either your catch or finally block.
Bosun

John Bateman
Ranch Hand

Joined: Mar 09, 2000
Posts: 320
I would use recursion.

Make a method
Ex. processLoop(int[] myIntArray, int startingElement)
Then in this method process your loop, in the event you get an exception like you described above, you can reset the value and recall the method with the new starting value.
ex:

If you are really gettting exceptions in a Collection or Array, then I would be more worried as to why its happenning and not trying to just fire the loop again. I'm saying this because I don't want to get flamed.

Hope this helps.

SOURCE CODE should be SURROUNDED by "code" tags.
Gaurav Chikara
Ranch Hand

Joined: Jun 09, 2000
Posts: 410
As per said by John
It is easy to recover from such exception but my case is different
I m using doPost method of a servlet
so in case if i try to call doPost again
it only can be called by two paramaeters
dopost(HttpServletRequest req,HttpServletResponse res)
so it goes into infinite loop
i have not been able to find a crack for it
very strange problem
Originally posted by John Bateman:
[B]I would use recursion.

Make a method
Ex. processLoop(int[] myIntArray, int startingElement)
Then in this method process your loop, in the event you get an exception like you described above, you can reset the value and recall the method with the new starting value.
ex:

If you are really gettting exceptions in a Collection or Array, then I would be more worried as to why its happenning and not trying to just fire the loop again. I'm saying this because I don't want to get flamed.

Hope this helps.[/B]

Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
While there are a million ways to acheive something,
you need to choose wisely why you want to use
a loop in a doPost method. This will only coverup your
real source of error. I am not a big fan of such code.
My $0.02.
If this is what you want to do, check this out:
(a modification to John's code)
regds.
- satya


[This message has been edited by Madhav Lakkapragada (edited March 14, 2001).]


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Gaurav Chikara
Ranch Hand

Joined: Jun 09, 2000
Posts: 410
Thnkx Madhav or satya
I use to read your replies when I was preparing for my Sun Certification
Any way I just got a crack out of this problem
I called another method within doPost
and i removed all the code of doPost and put in that method
and there i was able to do my recursion
and though exception is generated program functions normally
now how much do you put worth for this crack
i think at least 1$
Originally posted by Madhav Lakkapragada:
[B]While there are a million ways to acheive something,
you need to choose wisely why you want to use
a loop in a doPost method. This will only coverup your
real source of error. I am not a big fan of such code.
My $0.02.
If this is what you want to do, check this out:
(a modification to John's code)
regds.
- satya


[This message has been edited by Madhav Lakkapragada (edited March 14, 2001).]
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

You gotto do what you gotto do!
Good Luck.
Personally, what scares me is your comment
though exception is generated program functions normally.
I would disagree with you on the second half of your stmt.
Personal opinion.
regds.
- satya
Matts Smith
Ranch Hand

Joined: Feb 03, 2001
Posts: 113

Madhav: coding like that in a real program will get you fired. I would fire you without notice if I saw your code doing that.
get a book on algorithm or get a new career.
DAMN
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

Matt:
Thanks for the challenge, I hope to learn something new, now.
So, lets talk, why? Just curious to know why?
I do agree that getting an Algo book would help, but can't
wait....
Hope to hear from you (others are equally welcome).
regds.
- satya
Matts Smith
Ranch Hand

Joined: Feb 03, 2001
Posts: 113
When a RuntimeException is thrown in a program it means that either something is wrong in the design or code OR the environment or prerequisite for running the program are not correct.
With that said, it might be possible to fix this situation at runtime. If it is not possible then (in the case of a servlet like in this example) writing to a log file or even sending an e-mail message to the sysadmin might be an appropriate measure.
If you catch the exception and don't do anyhting about it then you are burying it in the code. A bug like that can affect thousands of users before it is discovered.
In your code example it is even worse than that. With the information I have, if and exception occurs, it will loop 10000 times,rethrowing the exception everytime. Usually it is a bad practice to try to recover from an exception by redoing the same code over and over. This approach might work when you are waiting for a ressource to be freed (frozen socket for example).
My suggestion to the gentlemen programmers is good design handles exception better than a catch clause.
Hope you get my point.
later
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

Matt:
Thanks for sharing your view. I truly appreciate it,
I mean it.
Having said that,
When a RuntimeException is thrown in a program it means that either something is wrong in the design or code OR the environment or prerequisite for running the program are not correct.
From my earlier post:
...you need to choose wisely why... Also,
This will only coverup your real source of error.
With that said, it might be possible to fix this situation at runtime. If it is not possible then (in the case of a servlet like in this example) writing to a log file or even sending an e-mail message to the sysadmin might be an appropriate measure.
If you catch the exception and don't do anyhting about it then you are burying it in the code. A bug like that can affect thousands of users before it is discovered.

From my earlier post:
.....This will only coverup your real source of error.
You said burying I said coverup.
In your code example it is even worse than that. With the information I have, if and exception occurs, it will loop 10000 times,rethrowing the exception everytime.
Usually it is a bad practice to try to recover from an exception by redoing the same code over and over. This approach might work when you are waiting for a ressource to be freed (frozen socket for example).

From my earlier post:
....I am not a big fan of such code.
My $0.02.
If this is what you want to do, check this out:

The consumer said:

it only can be called by two paramaeters dopost(HttpServletRequest req,HttpServletResponse res)
so it goes into infinite loop

My suggestion to the gentlemen programmers is good design handles exception better than a catch clause.
From my earlier post:
I would disagree with you on the second half of your stmt.
Personal opinion.
Hope you read the italisized second
half of the bold stmt
made by the consumer.
I definetly see where you are coming from. And when you read
my post carefully (though I might not have been very clear,
I apologise for that)
I hope I din't disappoint you.
Though I don't mean to take your comment personally, saying
that in a public forum like this IMHO (in my humble opinion)
was rude.
Again Thanks for your post I enjoyed it!
regds.
- satya
ps: Did you hear the saying Customer is the King. I think
Burger King or McDonalds' uses something like that.
Matts Smith
Ranch Hand

Joined: Feb 03, 2001
Posts: 113
Sure it was rude... But I'm the angry kind when it comes to bad code sorry about that.
My advice with clients (consumers as you call them) If they ask you to do something weird. Explain a better approach. I've been a consultant for 2 years now and one thing I learned :
When it comes to programming, clients are dumb. They don't know what they're doing. They hire you because they need technical advice. With that said, never in my career a client refused to implement one of my idea that took a better approach than the one he wanted (after explaining it).
Don't forget that in a real-life situation your name would be next to this code in the version control logs. If another programmer sees that, and apparently no other approach has been considered it will be a subject of discussion at lunch. Other programmers will lose respect for you. I don't know if you work as a programmer but in this domain, especially in consulting, it's all about reputation and attitude.
Sorry again for the rude post, but I just can't stand this kind of code. A good idea would have been to ask the consumer what EXACTLY he is trying t acheive. I'm sure a better solution would have come out.
until we meet again
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

When it comes to programming, clients are dumb.
This is precisely the type of stmt I donot wanted to make.
The only reason I got involved into this discussion is to
say

When a RuntimeException is thrown in a program it means that either something is wrong in the design or code OR the environment or prerequisite for running the program are not correct.

After having read the whole discussion, I understood that
this is NOT what they wanted to hear. They were looking for
a solution. And without knowing WHY they are looking for a
quick and dirty solution, maybe in my opinion, I didnot
want to say

When it comes to programming, clients are dumb. They don't know what they're doing.

....but I just can't stand this kind of code.
Trust me, neither can I.
Thanks for apologising, but thats' okay!
regds.
- satya
Gaurav Chikara
Ranch Hand

Joined: Jun 09, 2000
Posts: 410
Hi
What i intended to say was
that my program is handling the runtime exception and recovering from it
as far as user is concerned he is not aware whether program throwed exception and recovered as far as program is giving the required output
AfterAll RuntimeExceptions can be handled at runtime only
not at design time
Originally posted by Madhav Lakkapragada:

You gotto do what you gotto do!
Good Luck.
Personally, what scares me is your comment
[b]though exception is generated program functions normally
.
I would disagree with you on the second half of your stmt.
Personal opinion.
regds.
- satya[/B]

Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

Gaurav:
The point of debate is "Why are you trying to recover in
this manner ?"
You already know that you are getting an Exception. So,
investigate it (maybe add a input argument checking method)
rather than trusting the input, thowing Exception (sometimes)
and then attempting a recovery.
Sure you can produce the correct output to he user, but how
you produce it is also equally important.
Hope you get the point.
regds.
- satya
Gaurav Chikara
Ranch Hand

Joined: Jun 09, 2000
Posts: 410
I agree with you satya
and i told u or all the reason that why i am getting this exception as per my message posted March 14, 2001 12:34 PM
If i initialised that variable to 0 then runtimeexception will not be thrown but program will also not give the desired output
i did not found any alternative thats why i went this way
and according to me( may be i m wrong) there is no other alternative way for it


[This message has been edited by Gaurav Chikara (edited March 15, 2001).]
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040


- satya
Matts Smith
Ranch Hand

Joined: Feb 03, 2001
Posts: 113

The point of debate is "Why are you trying to recover in
this manner ?"


made my day.. you're hired
[This message has been edited by Matts Smith (edited March 15, 2001).]
Gaurav Chikara
Ranch Hand

Joined: Jun 09, 2000
Posts: 410
--------------------------------------------------------------------------------
As per said by satya
"The point of debate is "Why are you trying to recover in
this manner ?"
I will like to say
Runtime Exceptions are handled at runtime only
not at design time

[This message has been edited by Gaurav Chikara (edited March 15, 2001).]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Runtime Exceptions are handled at runtime only
not at design time

Usually, RuntimeExceptions allow you to discover, at run time, something that you missed at design time. Which means you should revisit the design. In this case - if you get an ArrayIndexOutOfBounds when you add something to a Vector, it probably means you are using the add(int index, Object obj) method, and you are trying to specify an index which is larger than the current size of the Vector. This suggests two things:
1) Use add(Object obj) instead, which will simply add an element to the end of the Vector, and will not throw an exception.
2) Whatever logic led you to think that you knew what index value to add the element at, it's wrong. Any use of this index in your program is now suspect - consider the value meaningless until you solve the problem.
One possibility is - if more than one thread is able to access your Vector at the same time, then you could get results like this. The methods of Vector are synchronized, true, but only when you're inside them. If you call two different methods of Vector, then strange things can happen in between unless you take extra steps to synchronize the whole process. I mention this because, while it's far from the only thing that could be wrong with your code, it can be one of the most difficult ones to diagnose if you haven't seen it before.
Good luck.
[This message has been edited by Jim Yingst (edited March 15, 2001).]


"I'm not back." - Bill Harding, Twister
Gaurav Chikara
Ranch Hand

Joined: Jun 09, 2000
Posts: 410
Thnkx Jim Yingst and Satya
After working on it for 14 more days since your last post
I finally improved my code so that it is not throwing ArrayIndexOutofBounds Exception
Now Satya how much will you think my code is worth
still $0.20 or more?

Originally posted by Jim Yingst:
Usually, RuntimeExceptions allow you to discover, at run time, something that you missed at design time. Which means you should revisit the design. In this case - if you get an ArrayIndexOutOfBounds when you add something to a Vector, it probably means you are using the add(int index, Object obj) method, and you are trying to specify an index which is larger than the current size of the Vector. This suggests two things:
1) Use add(Object obj) instead, which will simply add an element to the end of the Vector, and will not throw an exception.
2) Whatever logic led you to think that you knew what index value to add the element at, it's wrong. Any use of this index in your program is now suspect - consider the value meaningless until you solve the problem.
One possibility is - if more than one thread is able to access your Vector at the same time, then you could get results like this. The methods of Vector are synchronized, true, but only when you're inside them. If you call two different methods of Vector, then strange things can happen in between unless you take extra steps to synchronize the whole process. I mention this because, while it's far from the only thing that could be wrong with your code, it can be one of the most difficult ones to diagnose if you haven't seen it before.
Good luck.
[This message has been edited by Jim Yingst (edited March 15, 2001).]

Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

Hey!!!
I think you got me wrong in the first place.
I am not saying your code is $0.02, but my suggestion
is $0.02 worth for your purpose, incase you are not
familiar with that term, its American I guess.
Just clarifying.
Your code is always worth a $ 1 million (if not more)
to you.
Anyways, its nice that you could fix the root cause
of the problem. Fix it right and fix it once.
I hate the quick and dirty ways of fixing code.
Good Luck.
regds.
- satya
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can we send the control from finally to try?
 
Similar Threads
Return from finally
IBM Mock Exam: Try Catch Finally ?
Exception Handling
exception
Return Statement and Finally