• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

basic exception handling

 
Ranch Hand
Posts: 164
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone take time to explain how to add a try catch block around a component such as a text field or combobox so that any errors can be notified to the user

I have tried following what netbeans does but then i do not know what ti put in the exception clause  
e.g.

most of the time these exception happen and have little or no effect on the out come but it would be nice to fire off something say on the lines of a joption message box so the error is know about
 
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

peter m hayward wrote:. . . how to add a try catch block around a component such as a text field or combobox . . .

That doesn't make sense. You do not apply try‑catch to a component, but to lines of code inside a method.
Are you referring to initialising a field in a constructor or initialiser block?

Whichever code you have, you can note that it might suffer an error leading to an exception (small e) being thrown by surrounding it with try {...}, and you then have two options (which can be combined if necessary). You can handle the exception in a catch clause or you can instruct code to run irrespective of an exception with a finally clause. If you aren't handling the exception here and now, you can allow it to propagate (unchecked exceptions) or you can notify whichever code called the current code by declaring the exception (checked exceptions) with a throws XYZException declaration. Remind yourself of the details in the Java™ Tutorials. Regard a throws declaration as meaning, “This method or constructor might suffer an XYZException, but we don't think this is the right place to handle it if it occurs, and any users of this code must work out how to handle it for themselves.”
Now, you appear to be doing exactly that in the code you posted. Obviously your IDE defaults to writing a logging statement in its catches. You can of course change that; there is probably a project configuration file which contains your options and there will be a menu to change those options and you can specify a different default contents for a catch. What is happening is that your method call (line 4) declares that it might throw an SQLException and you have to handle that in lines 5‑7. If you change line 6 to something like ex.printStackTrace();, you will get different behaviour from that code block.
Now, you can declare Exceptions (large E) from methods and constructors. If you suffer an exception which isn't handled from an initialiser block, you may find the JVM changes it to this sort of exception, which isn't normally caught. I don't know whether that is what you meant to ask.

I am not convinced that you have arrived in the right forum: let's move you to where we usually discuss things “basic”.
 
peter m hayward
Ranch Hand
Posts: 164
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry i see that was a bit misleading  here is an example of what i am try to explain


the action of this code is to seperate out the author and title from a string containing both e.g.   Carrie by Stephen King  it looks for the word "by"  and as you can see from the code there are a few value that are common to all these string so The Golden Straw by Catherine Cookson would also follow the rules however sometimes the will be some other factors that crop up such as Babysitters Club by Ann M. Martin
now as the word baby contains by i get and array out of bounds error and this happens without me knowing about because i have not put in place any try catch to deal with it as i am struggling to understand what the error e would be where as the mysql errors are know and in any case  netbeans put the try catch in for me i then go to the catch block and put some code to deal with the issue should it happen
hope this goes some way to explaining my confusion and difficulty

 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i get an array out of bounds error


This could be prevented by using an if statement to test if the index is in bounds before using it.

Please copy the full text of the error message and paste it here so we can see what you are talking about.
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without going deeper into the forest, why your method does so much stuff? ~80 lines of code.

Method name jLabel1MouseClicked. Choice of such method name is very poor. I guess you could find an error more easily if you'd start decomposing your long method into several small methods.
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'd want the String in your call to indexOf() to require a space on either side of the "by".
splitHere = AuthorTitleString.indexOf(" by ");
 
peter m hayward
Ranch Hand
Posts: 164
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Without going deeper into the forest, why your method does so much stuff? ~80 lines of code.

Method name jLabel1MouseClicked. Choice of such method name is very poor. I guess you could find an error more easily if you'd start decomposing your long method into several small methods.


yes the name was picked by netbeans you notice i changed all the others must have missed this one. for some reason you seem to think i am say there is an error in the code, which the is not the problem lies in the string that comes in from external source which do not fall the typical rules which upon examination is down to the individual how typed the data in in the first place for instance some pepole type thing all caps where others capitalist each word others leave 2 spaces between title and  author  while others only then some put slashes between so the data is the main source of problems not the action of the code
here is another example of the data

"Magician (1982)(A book in the Riftwar series)A novel by Raymond E Feist"
after passing this through the method you get

title = Magician  (A   Riftwar Series) which is wrong
author = Raymond E Feist  which is correct

so life is never as simple as you would like it if you have any idea as to hope to get consistence i would implement it at once
this problem as been a problem from the word go
 
peter m hayward
Ranch Hand
Posts: 164
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:You'd want the String in your call to indexOf() to require a space on either side of the "by".
splitHere = AuthorTitleString.indexOf(" by ");



yes i tried that and got into a worse mess, the example was purely that i had all ready solved that one the main issue is the inconsistency in the external data so hence the need to catch errors and make the user aware of them by this bring me back to the original problem what do you as k the try catch to look out for ?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think try/catch is a solution for parsing problems with substring and indexOf methods.  An if statement can prevent the use of invalid indexes.

Can you post some specific examples of input Strings that the code can not handle and the stack traces for when the code has problems with those Strings?
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

peter m hayward wrote:yes i tried that and got into a worse mess


Good that you agree there is a mess already.

Your problems are understandable. What I say is, if I were you, I wouldn't bother to fix/improve some sort of stuff if the whole code you have is in such condition.
What I'd think you really need to do now, is to start decomposing your huge method into several small methods where each of those would be responsible for a single task.

Once you'll have that accomplished in a form similar to (using dummy example), you'd be able to understand, where you actually need to hook try; catch blocks and what there might need to be.

Example is very abstract, but this is what you supposed to be trying to achieve. Your current code is too complicated. Too much stuff going on which you can't understand. It isn't responsibility of this jLabel1MouseClicked method to do all that routine you are doing now.

   
 
peter m hayward
Ranch Hand
Posts: 164
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i agree and will take on the task of making the huge method in to smaller ones
thank you all for pointers
pete
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic