This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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?
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
Joined: Sep 14, 2008
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.
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 ]
Joined: Sep 14, 2008
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.