title: Try...catch vs if...else for exceptions - Java
body: <p>What if I want to take user input from the <code>args[0]</code> array, but just in case I (the user) forgot to define it, I wanted a prompt to come up - is it better to use an <code>if</code> block to determine whether the array item(s) is empty or not, or to catch the exception? So, is this</p>
  
  <pre><code>public class Stuff {
      static Scanner input = new Scanner(System.in);
      public static void main(String[] args) {
          String foo;
          if(args.length &gt; 0) {
              foo = args[0];
          }
          else {
              foo = getString("Input? ");
          }
      }
      public static String getString(String prompt) {
          System.out.print(prompt + " ");
          String answer = input.nextLine();
          return answer;
      }   
  }
  </code></pre>
  
  <p>better or worse than</p>
  
  <pre><code>public class Stuff {
      static Scanner input = new Scanner(System.in);
      public static void main(String[] args) {
          String foo;
          try {
              foo = args[0];
          }
          catch(ArrayIndexOutOfBoundsException e) {
              foo = getString("Input? ");
          }
      }
      public static String getString(String prompt) {
          System.out.print(prompt + " ");
          String answer = input.nextLine();
          return answer;
      }   
  }
  </code></pre>