Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

try catch error handling

 
M Shaw
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
M Shaw
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48645
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic