• Post Reply Bookmark Topic Watch Topic
  • New Topic

When to use Try/Catch, when not to.  RSS feed

 
Andrew Bedley
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A java book I have encourages me to use try/catch only for when the exception is "truly exceptional." They say that in data entry, you should prevent the exceptions from being thrown. I merrily used the Scanner.hasNextDouble() method in console applications. The book kind of left me high and dry though on AWT GUI apps. I went ahead and attached the Scanner to the string gleaned from an AWT textField, and it worked! My questions, however, are:

1. The examples I get from the web seem to use Try/Catch. Do people not try and avoid exceptions like the book recommends?

2. Am I using Scanner in a manner that it was not intended? What do I use for an AWT program? My class doesn't trust us with Swing yet, but if I graduate to Swing, can I use the same techniques as AWT?

2. When studying Javascript, I made use of w3schools.com as a good, solid reference. I don't find anything as easy to use in Java, except for the complicated
javadocs, and you guys. What sites give legit answers? I scour the web, and everybody has slightly different answers - it's hard to know who is best.

Thanks,

Andrew
 
jake dickens
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew Bedley wrote:A java book I have encourages me to use try/catch only for when the exception is "truly exceptional." They say that in data entry, you should prevent the exceptions from being thrown. I merrily used the Scanner.hasNextDouble() method in console applications. The book kind of left me high and dry though on AWT GUI apps. I went ahead and attached the Scanner to the string gleaned from an AWT textField, and it worked! My questions, however, are:

1. The examples I get from the web seem to use Try/Catch. Do people not try and avoid exceptions like the book recommends?

2. Am I using Scanner in a manner that it was not intended? What do I use for an AWT program? My class doesn't trust us with Swing yet, but if I graduate to Swing, can I use the same techniques as AWT?

2. When studying Javascript, I made use of w3schools.com as a good, solid reference. I don't find anything as easy to use in Java, except for the complicated
javadocs, and you guys. What sites give legit answers? I scour the web, and everybody has slightly different answers - it's hard to know who is best.

Thanks,
Andrew


try and catch is used in loops also some don't require loops my opinion is this is just depends on what you want to do.

2. scanner has next double gives you a number like this 3.22 AWT classes are for gui which is graphical user interface basically putting pictures on the JFrame/applet/JPanel I use to watch The New Boston on youtube he has about 120 videos on java which may give you answers you seek. He sets up with how to install the jdk and set the javac also he breaks it down for the novice to learn.

http://www.youtube.com/watch?v=Hl-zzrqQoSE&feature=list_related&playnext=1&list=SPFE2CE09D83EE3E28

also he had intermediate java as well.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew Bedley wrote:1. The examples I get from the web seem to use Try/Catch. Do people not try and avoid exceptions like the book recommends?

If exceptions can be prevented by a little checking this is preferred. For example, before calling Scanner.nextXXX() (with XXX being Double, Int, etc) you should check first using Scanner.hasNextXXX().

2. Am I using Scanner in a manner that it was not intended? What do I use for an AWT program? My class doesn't trust us with Swing yet, but if I graduate to Swing, can I use the same techniques as AWT?

Listeners, using one single thread (the Event Dispatcher Thread), etc - that's also in Swing. It's only the components you need to replace - JButton instead of Button, JFrame instead of Frame, etc.

2. When studying Javascript, I made use of w3schools.com as a good, solid reference. I don't find anything as easy to use in Java, except for the complicated
javadocs, and you guys. What sites give legit answers? I scour the web, and everybody has slightly different answers - it's hard to know who is best.

http://download.oracle.com/javase/tutorial/
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For GUIs, you might try getting the text from the text component, and applying a regular expression to it. That can show you whether it is in the correct format for a double, for example.
 
Andrew Bedley
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank-you for the help so far everybody. Somebody mentioned using regular expressions, which I see could be useful. I have seen that style of doing things on the on the web. I have seen the try/catch method. I am curious why I have not seen anything using the Scanner class. It worked well for me, but I am missing something? Is there some performance hit? Is there something in the Java Ranch Style Guide that pooh-poohs this?

I have this event listener:

The above event listener calls this method:


This method uses the scanner class, and it seems to work real well. So why do the examples I find on the web ignore the scanner class?

Thanks,

Andrew
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably because Scanner was added to the API with Java 1.5, and the examples were written before that?

I have to admit that even that Java 1.5 was released back in 2004, I still haven't got around to learning how to use it. That's most likely because as a professional programmer I almost never write code which needs to use a Scanner.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!