• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Catching exception break a loop.

 
Ranch Hand
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
het there.

This:


is my code.

the problem is that this:

loop is getting me like this:

the reason is that, when system throws the "ArrayIndexOutOfBoundsException", it just print the StackTrace, and SHOULD continue the loop normally.

but....

it doesn't.

when the system gets the ArrayIndexOutOfBoundsException, the loop(for) break; the next itens (in the array) aren't even handled by the program..


anyone knows if there is a problem in catching an exception inside a loop?
 
Marshal
Posts: 80761
487
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Difficult to understand this problem, but I think you are making things far too complicated.

Where are you getting the out of bounds exception? You ought not to catch such an unchecked Exception at all, but find out why it occurring and prevent it. You have two for loops which appear correctly formed, so they ought not to throw exceptions.
You are creating an ArrayList[] which looks strange; an ArrayList ought to be parameterised (ArrayList<Foo> ) and you ought not to create an array of parameterised objects. It is however easy to create a List<List<Foo>>.

Why are you using a switch (which ought to have 12 "break;" statements, please, not 11)?
You can set up the Mes variable much more easily like thisYour populating your data from a pair of HashMaps looks very complicated. What you are passing doesn't look like a typical use for a Map, but an object in its own right.I don't understand where you get the ArrayLists from, but that looks like something which ought to be a field of the ChartData object too.

This ChartData object will be very large!

Your 5 Soma numbers and the number for the month look like values which ought to be in their own object too.

I can't see just looking at your code why you are losing the loop. I suspect the Exception occurs because you are trying to get 6 objects out of an ArrayList which doesn't contain 6 objects, but you should be able to work that out from the stack trace. Are you sure it is the ArrayIndexOutOfBoundsException causing the trouble; you have two catches (NullPointer and IllegalArgument) which you ought not to use, but include System.exit(-1) and your catch (IOException) also includes an exit call.
[ November 10, 2008: Message edited by: Campbell Ritchie ]
 
Lucas Franceschi
Ranch Hand
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
Welcome to JavaRanch

Difficult to understand this problem, but I think you are making things far too complicated.

Where are you getting the out of bounds exception? You ought not to catch such an unchecked Exception at all, but find out why it occurring and prevent it. You have two for loops which appear correctly formed, so they ought not to throw exceptions.
You are creating an ArrayList[] which looks strange; an ArrayList ought to be parameterised (ArrayList<Foo> and you ought not to create an array of parameterised objects. It is however easy to create a List<List<Foo>>.

Why are you using a switch (which ought to have 12 "break;" statements, please, not 11)?
You can set up the Mes variable much more easily like thisYour populating your data from a pair of HashMaps looks very complicated. What you are passing doesn't look like a typical use for a Map, but an object in its own right.I don't understand where you get the ArrayLists from, but that looks like something which ought to be a field of the ChartData object too.

This ChartData object will be very large!

Your 5 Soma numbers and the number for the month look like values which ought to be in their own object too.

I can't see just looking at your code why you are losing the loop. I suspect the Exception occurs because you are trying to get 6 objects out of an ArrayList which doesn't contain 6 objects, but you should be able to work that out from the stack trace. Are you sure it is the ArrayIndexOutOfBoundsException causing the trouble; you have two catches (NullPointer and IllegalArgument) which you ought not to use, but include System.exit(-1) and your catch (IOException) also includes an exit call.




ok! that 12 cases really (no, i mean, Really.) made me feel like a newbie in java (actually i am.)
but!

my real problem is not solved yet.
I still getting off the loop when i get the exception.

and i've been using this forum for years, i just have never registered...
and the i'm brazillian, so the name of the variables and some comments may be in portuguese.
 
Campbell Ritchie
Marshal
Posts: 80761
487
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wondered about the language; I thought it wasn't Italian.

I don't know why you are not repeating the loop, unless it is a different Exception. Sorry.

Anybody else?
 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just ran the code myself (guessing what params would be) and it ran without error. Which line is actually causing the ArrayOutofBounds? As Campbell suggests you should not really just catch this exception and continue, it is important to solve the root cause.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lucas,


the reason is that, when system throws the "ArrayIndexOutOfBoundsException", it just print the StackTrace, and SHOULD continue the loop normally.

but....

it doesn't.



Actually it should continue the for loop - and probably, it does . Try something like the following and you will see:



I would suggest you to insert some println() statements in your code in order to check exactly when the exception is happening (or better yet if you have an IDE with a debugger).

Good luck.
 
Lucas Franceschi
Ranch Hand
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually. i've already debugged the code, and the line that trows the exception is

this last row:


martijn,
the exception is trown with one specific case in the params structure, it's also why i'm trying to solve this without changing the root cause of this.

the root cause come from the parameters structure, but this params come from a database that cannot be changed, as the name suggests i only need to create a LineChart, only manipulating the data, not changing it on the database.

that's also why i need to pass through this exception just ignoring that line presence, there would be another ways to pass throguht this exception, but i'm afraid I would have to change a lot of other classes, such as the structure of this one.
 
Lucas Franceschi
Ranch Hand
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh, and thanks for all of you guys, i appreciate the help.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only way I can see for the loop not to continue when the exception is thrown is if the exception is thrown on the last iteration through the loop. Check the value of 'i' when the exception is thrown and compare it to dados.size().
 
Lucas Franceschi
Ranch Hand
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Problem Solved. Thank you for the help People!

because I know you ranchers are curious, I will tell you what was the problem:

looks like the loop was stopping because there were no other value to go for!

I was going throught MesPorDado just as if I was in dados, and that was my problem.

he only thing I did to solve this, was having some sleep, drinking a coffee and doing something else. that helped me thinking off the problem, and then I just saw that it was right in front of my eyes...

I do appreciate that you take your time to help. it was a great difference in solving this one.

i'm sorry if my horrible english, or my javaranch newbieness was bad, but I try to do what I can.

so i insist to say, do not waste your time with this anymore, it is ALREADY SOLVED. and save your time for those who really need.

thank you all for helping.
 
Campbell Ritchie
Marshal
Posts: 80761
487
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Success! Well done.
 
Lucas Franceschi
Ranch Hand
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, thanks for the help.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic