title: how to prevent division by zero without handling exception java
body: <p>I have two classes: <code>Student</code> and <code>University</code>. </p>
  
  <pre><code>public class University {
  
  private String name;
  private List&lt;Student&gt; students;
  
  public University(String name) {    
      this.name = name;
      this.students = new ArrayList&lt;Student&gt;();
  }
  </code></pre>
  
  <p>and there is a function to calculate the average score:</p>
  
  <pre><code> public double average() {
      double average = 0;
  
      for(Student st: students) {
          average += st.getKnowledge();
      }
  
      try { 
          return average/students.size();
      }
      catch(ArithmeticException e) {
          System.out.println("Oops, there is no student studying");
          return 0.0;
      }
  }
  </code></pre>
  
  <p>So if there is no students in the list, i get division by zero which i tried to handle with a <code>try\catch</code> block. 
  And I wonder if there is any way to simplify this checking in order to avoid the <code>try catch</code> block? Of course, I could implement it like this:</p>
  
  <pre><code>public double average() {
      double average = 0;
  
      for(Student st: students) {
          average += st.getKnowledge();
      }
  
      if (students.size() == 0) 
          throw new IllegalArgumentException("Oops, there is no student studying");
  
      return average/students.size();
  }
  </code></pre>
  
  <p>either</p>
  
  <pre><code>if (students.size() == 0) 
          System.out.println(""Oops, there is no student studying"");
  </code></pre>
  
  <p>Or probably there is no point in checking it because if <code>students</code> list is empty then it simply wont enter the loop?</p>
  
  <p>I know the question is easy but I am only styudying so I'd like to know in the future which way is better to pick. 
  And also perhaps there is a hidden problem or something? with boundaris values or whatever? </p>
  
  <p>thanks in advance.</p>