title: How to throw an exception from a recursive function in scala that returns Int
body: <p>I've recently started learning scala and as a part of an assignment, I need to write a function with following requirements: it has to be recursive and throw an exception in case of empty list. Tried this code, but the exception is always thrown.</p>
  
  <pre><code>def max(xs: List[Int]): Int =
    if (xs.isEmpty) {
      throw new NoSuchElementException("empty list")
    } else {
      if (xs.head &gt; max(xs.tail)) xs.head else max(xs.tail)
    }
  }
  </code></pre>
  
  <p><strong>edited:</strong> sorry for a mistake, of course the exception needs to be created with new. However, the function always fails with exception.
  tried require(), but it returns a Unit type, hence the compiler says it cannot be put in there.
  Is there a simple way of throwing an exception from a recursive function in scala whithout resolving to Try, Option and others?</p>