1
void input () throws IOException 
{
   .......
}

This works fine with both Scanner & Buffered-Reader

void input ()
{
 .......
}

But why this shows Compilation error in Buffered Reader & works fine in Scanner.

If any error occurred it should be shown in Run- time ,but why it is mandatory to explicitly tell the compiler ,in case of Buffered- Reader and not in case of Scanner

  • Scanner can perfectly well be used on strings, rather than InputStreams; it would be inconvenient if code using Scanner to parse strings had to declare or catch IOExceptions which could never be thrown in the first place. – kaya3 Feb 12 '20 at 04:33

4 Answers4

4

It isn't about the class you use, but the methods you are calling and what exceptions they throw.

Some (most) BufferedReader methods throw IOException which is a 'checked' exception. This means you have to specify when your method throws it (ie. doesn't catch it).

From a cursory glance, it appears that the methods in Scanner throw 'unchecked' exceptions (exceptions that derive from RuntimeException). These don't have to be specified if your method throws them (doesn't catch them).

The key is that it depends on which exceptions (checked vs unchecked) are thrown by the methods you are calling, and whether you catch those exceptions or let them bubble up to the calling code as to whether or not you need to specify them in the signature of your method.

Edit: To answer your question about compiler errors, the compiler is forcing you to either catch or explicitly declare any exceptions that are thrown by your method (or from methods you are calling).

Jason
  • 11,744
  • 3
  • 42
  • 46
1

The reason is BufferedReader methods throw IOException, you can check here: Docs: BufferedReader

Adding to above, FileReader constructor in below example also throwsFileNotFoundException

 BufferedReader in = new BufferedReader(new FileReader("foo.in"));

IOException and FileNotFoundException both are checked exception and must be handled or exception handling should be delegated to the caller method (in your case, input()) by using throws

Read this for explanation: https://javarevisited.blogspot.com/2011/12/checked-vs-unchecked-exception-in-java.html

On the other hand, Scanner method you are calling may not be throwing any checked-type exception Docs: Scanner

Govinda Sakhare
  • 5,009
  • 6
  • 33
  • 74
1

Scanner isn't intended only for IO operations.

Throwing an IOException would be confusing for clients using a scanner for non-IO purposes. A previous answer of mine which focuses on Scanner shows an example of using Scanner to scan a String:

class ScannerWrapper implements Iterable<E> {
    public Scanner scanner;

    public ScannerWrapper(Scanner scanner) {
        this.scanner = scanner;
    }

    public Iterator<String> iterator() {
        return scanner;
    }
} 

Scanner scanner = new Scanner("one,two,three");
scanner.useDelimiter(",");
ScannerWrapper wrapper = new ScannerWrapper(scanner);

for(String s : wrapper) {
    System.out.println(s);
}

You can still monitor any IOExceptions thrown by the underlying IO component via Scanner#ioException():

Returns the IOException last thrown by this Scanner's underlying Readable. This method returns null if no such exception exists.

Vince
  • 14,470
  • 7
  • 39
  • 84
0

Exceptions should be caught or thrown. In other words, you should put methods that can throw Exceptions in a try/catch/(finally) block, or tell the compiler that it's OK if this method throws certain types of Exceptions.

NomadMaker
  • 447
  • 1
  • 5
  • 9