This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes try catch error handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "try catch error handling" Watch "try catch error handling" New topic
Author

try catch error handling

M Shaw
Greenhorn

Joined: Sep 14, 2008
Posts: 11
I am writing a program and we have to use try and catch for errors from input files. I'm using method from the BufferedReader class and there is something funky going on because of my try/catches. Does try and catch go into a separate method of their own, or do they have to be in a main method?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

A try/catch block is placed inside a method body. In general, the try block encloses code that might throw something you want to catch.

What is the "funky" thing going on with your code?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
M Shaw
Greenhorn

Joined: Sep 14, 2008
Posts: 11
maybe it's not really funky, I just really don't understand what's going on. I've created a BufferedReader object and instantiated it, and I call it throughout my code using the "read()" method. I'm using it in various methods throughout my code. I'm getting errors when I compile that say "cannot find symbol variable buffReader" (buffReader is my BufferedReader object) When I move my try/catch around those errors don't come up anymore, but I get errors about my try/catch.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Without seeing any of your code, we can only guess...

It sounds like your variable "buffReader" isn't declared in the proper scope, which is why you're getting the "cannot find symbol" error.

It also sounds like when you're moving your try/catch blocks around, you are putting them in the wrong places (for example, outside of a method body). This will generate errors like "illegal start of type" or "identifier expected." When this happens, your "cannot find symbol" errors might not be generated, but that's only because the compiler is not getting far enough to recognize those. In other words, you're just "hiding" the earlier errors -- not fixing them.

If you post your code, we can tell for sure.

For details on exception handling, see Java Tutorial - Exceptions.
[ September 16, 2008: Message edited by: marc weber ]
M Shaw
Greenhorn

Joined: Sep 14, 2008
Posts: 11
Thanks for the help! I just moved them around somemore and actually got them in the right place and now it is compiling!! Looks good so far.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Please check carefully in the API what the read() method does; you may end up with a single keystroke read as a byte.
You may find readLine() more useful.
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3219
check this site http://kickjava.com/933.htm. Make sure that you close the file handles in a finally block.

[ September 17, 2008: Message edited by: arulk pillai ]
[ September 17, 2008: Message edited by: arulk pillai ]

Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: try catch error handling
 
Similar Threads
Please Help with the flow of this code
Exception handling
meaning of return; only
Can Java catch more than one exceptions from the single try_catch block?
TRY CATCH FINALLY BLOCKS