title: Java Scanner Good Practice
body: <p>I am reading from a file, and I want to handle the exceptions using try/catch blocks.
  I wrote my code. However, eclipse gave me an error with just declaring my scanner object, and I had to initialize it to null. Below I wrote two versions of my code. Which is considered a better practice? Also, is it better to use try/catch block than just throw the exception at the end of the constructor/function header?</p>
  
  <p>Code version #1:</p>
  
  <pre><code>java.util.Scanner in = null;
  
  try {
      in = new java.util.Scanner (f);
      /* use scanner */
  } catch (FileNotFoundException e) {
      System.err.println("File was not found. Make sure the file exist.");
      System.err.println("Message: " + e.getMessage());
  } catch (IOException e) {
      System.err.println("File could not be opened.");
      System.err.println("Message: " + e.getMessage());
  } finally {
      in.close();
  }
  </code></pre>
  
  <p>Code version #2:</p>
  
  <pre><code>try {
      java.util.Scanner in = new java.util.Scanner (f);
      /* use scanner */
      in.close();
  } catch (FileNotFoundException e) {
      System.err.println("File was not found. Make sure the file exist.");
      System.err.println("Message: " + e.getMessage());
  } catch (IOException e) {
      System.err.println("File could not be opened.");
      System.err.println("Message: " + e.getMessage());
  }
  </code></pre>