5 Replies - 16313 Views - Last Post: 12 October 2009 - 04:01 PM Rate Topic: -----

#1 harish_mca06  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-October 09

Difference between Scanner class and BufferedReader class

Posted 12 October 2009 - 07:31 AM

why it is necessary to throw IOException while using BufferedReader class methods but we don't throw IOException when we use Scanner class methods?
Is This A Good Question/Topic? 0
  • +

Replies To: Difference between Scanner class and BufferedReader class

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10466
  • View blog
  • Posts: 38,789
  • Joined: 27-December 08

Re: Difference between Scanner class and BufferedReader class

Posted 12 October 2009 - 07:44 AM

It isn't really "necessary" to throw an IOException when using a BufferedReader. Though the reason you use this particular exception is because that is what all except maybe one or two of its methods throw, excluding constructors. A Scanner, on the other hand, throws fewer exceptions within its methods. It may throw an IOException once or twice.

I'm also going to touch on the necessity of throwing an exception. If you, the programmer, know that the program could be reading in a file that could throw an error, then you can handle it in one of 2 ways. Either have that method throw an exception for another segment to handle later or use a try-catch statement then to handle the error.

For more information on Scanner and BufferedReader:
http://java.sun.com/...eredReader.html
http://java.sun.com/...il/Scanner.html
Was This Post Helpful? 0
  • +
  • -

#3 smacdav  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 177
  • Joined: 06-June 09

Re: Difference between Scanner class and BufferedReader class

Posted 12 October 2009 - 11:04 AM

Actually, Scanner will never throw an IOException specifically, although a couple of its constructors will throw a FileNotFoundException, which is a subclass of IOException. (I know that technically makes it an IOException, but it's indirect.)

For the most part, Scanner doesn't throw unchecked exceptions (though it technically should sometimes). This is partly historical: the Scanner class was originally developed as a teaching tool. It was intended as an easy way for beginners to be able to take keyboard input without having to deal with handling (or declaring) checked exceptions and such. For this reason, in many situations where it would make sense to throw some sort of IOException (a non-integer being read by nextInt, for example), it doesn't.

In general, checked exceptions (the kind that you must either declare or handle with a try-catch construction) are thrown for things that are outside of the programmer's control; missing files and improper keyboard input from a user are two examples. Unchecked exceptions, on the other hand, are generally the result of program logic errors. A properly written program should never throw an unchecked exception. (The last sentence doesn't really hold if the program uses Scanner, however, since it throws unchecked exceptions when it should throw checked exceptions, as noted before.)

To specifically address the OP, you need to declare or handle the IOException when using BufferedReader because many of its commonly used methods throw an IOException, and IOException is a checked exception. You don't need to do so when you use a Scanner because it does not throw an IOException or any other checked exception (unless you happen to use one of the two constructors that throw a FileNotFoundException).
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Difference between Scanner class and BufferedReader class

Posted 12 October 2009 - 03:52 PM

View Postmacosxnerd101, on 12 Oct, 2009 - 06:44 AM, said:

It isn't really "necessary" to throw an IOException when using a BufferedReader.

You think so ? :)
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10466
  • View blog
  • Posts: 38,789
  • Joined: 27-December 08

Re: Difference between Scanner class and BufferedReader class

Posted 12 October 2009 - 03:57 PM

View Postpbl, on 12 Oct, 2009 - 04:52 PM, said:

View Postmacosxnerd101, on 12 Oct, 2009 - 06:44 AM, said:

It isn't really "necessary" to throw an IOException when using a BufferedReader.

You think so ? :)


I prefer to handle exceptions when they occur rather than putting them off until later. To me, it is easier to debug or handle errors on the ground level rather than waiting until it is entangled with a lot of other code.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Difference between Scanner class and BufferedReader class

Posted 12 October 2009 - 04:01 PM

so you need an exception handler anyhow
you just move where the exception is catch
things that you do not have to do with a Scanner which was this DIC original question

This post has been edited by pbl: 12 October 2009 - 04:02 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1