title: Constructor handling exception and using this keyword Java
body: <p>I have two constructors for my class, one that takes <code>File</code> object and the other takes a <code>String</code> object, and I want to use the <code>this</code> keyword. The function with the implementation is the one with <code>File</code> as parameter, and the one with <code>String</code> will call <code>this</code>. Now I want to check for exception in the constructor that takes <code>String</code> but I get error, that <code>this</code> should be the first line. How can I check for errors then call <code>this</code>.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public Test (String filename) {
      if (filename == null)
          throw new NullPointerException("The String you entered is null.");
      if (filename.isEmpty())
          throw new IllegalArgumentException("The String you entered is empty.");
  
      this(new File(filename)); // error
  }
  
  public Test (File f) {
      /* implementation here */
  }
  </code></pre>
  
  <p>This is the exact error: <code>Constructor call must be the first statement in a constructor</code></p>