• 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
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

try-catch logic design issue

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rather than long explain, I am writing the sample code.
And I wonder how should I design this or is it a good design?

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
catch specific possible exception instead of java.lang.Exception and what is the significant of continue in your catch block ? regardless continue the control flow goes to the increment part of you for loop.

 
pamir sonmez
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if there is an error, the jobs will not be done after try-catch block
and in for loop i will take another object from list
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your solution should work, but why you use an exception? You can do the same with an if-else line of code. I dont know but using an exception seems odd to me in this particular case.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pamir sonmez wrote:if there is an error, the jobs will not be done after try-catch block


do you mean if any exception in try block then statement after catch block wont get execute? are you sure ?

<edit>
you doestnt put any code after the continue; but you put before continue.
so in this case you can put simply


</edit>
 
pamir sonmez
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy please tell something logical rather than smileys ))))))

I mean that if there occurs an exception while I am getting the valueof string to the enum, program enters the catch block and then program goes out the catch block and goes its own way after catch block. Ooop what I want is, damn stupid programme if you enter the catch block do not process your flow with this object, go the for loop increment i and get another objecct from list and start your job. hell yeah...
am I clear now???

thanks frank braunstein,
the reason I am using exception because I have to do.
I am reading a string from file and I am getting its enum value with valueOf(...) method, if string matches wth enum no problem.
But however, if string is written wrong, valueOf(..) method throws an exception. So I don want to use a long if block if this is equal to this, so it that enum.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cool

pamir sonmez wrote:am I clear now???.


no, still you dont answer my previous question . did you test your code as I suggest ?
 
pamir sonmez
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy
Thanks, but you do not understand what I mean or you do not read exactly what I wrote so you are asking same question again and again.
I am implementing the program flow as I wrote the code before, when program enters catch block, continues to the for loop from start of flow by incrementing i(variable) and if it does not enter catch block, I mean no exception occurs, goes to the end of flow and then goes to the start of flow by incrementing i(variable). I know the logic behind try-catch block.

The most important thing I want to ask is there any efficient or correct way of using valueOf(..) function for enumerations.
And In valueOf(string) function, if string is not an enum, how could I handle it? because it throws an exception and enters catch block.
And I have to stop the execution of flow if catch block is entered, and program must continue the for loop.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's really not clear to me what you're trying to achieve, but if you want to exit the loop when there is an exception, put the loop inside the try block (so the catch is outside the loop). If you want to continue looping after an exception, put the try...catch inside the loop.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a reason you don't put the rest of your processing logic in the try block? That would seem to be the most straightforward approach to avoid the dangling continue.


 
Good heavens! What have you done! Here, try to fix it with this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic