Java Streams  «Prev 

Checked Exceptions in a try-catch block


The is a checked exception, so you will need to wrap all calls to this method in a try-catch block.

try {
  int[] b = new int[10];
  for ( int i = 0; i < b.length; i++) {
    int datum =;
    if (datum  == -1) 
    b[i] = datum;
catch (IOException e) {
  System.err.println("Couldn't read from!");

File operations can typically cause unexpected situations that the program is not capable of handling (for example, if we try to open a file for reading, and specify a filename that does not exist). Such situations are called exceptions. They are classified according to the type of inconvenient that has happened, and they must be handled by the program by inserting suitable Java code For this unit, it is sufficient to know that the methods that make use of statements that can generate an exception must declare this.
For example, the methods for opening a file for reading can generate an exception of type IOException. Therefore, all methods that call such methods must declare explicitly that they can themselves generate an exception of that type. The type of exception that a method can generate is specified in the throws clause, which must be added to the method declaration before the body of the method. For example:
public static void main(String[] args) throws IOException {
	// code