Institute of Computer Science
  1. Courses
  2. 2018/19 spring
  3. Object-Oriented Programming (Narva College) (LTAT.NR.003)
ET
Log in

Object-Oriented Programming (Narva College) 2018/19 spring

  • Home
  • Materials
  • Java Glossary
  • Source Example
  • Cheat sheet (S1-S6)
  • Grading
  • Links
Session 11

Throw exception

Before we are going to proceed with the exception handling, let's have a look at how it is possible to break the program flow with own usr friendly message when an exception occurs:

static int factor(int n) {
  if (n < 0)
    throw new IllegalArgumentException("N cannot be negative!"); // an exception is thrown
  int result = 1;
  for (int i = n; i > 1; i--)
    result = result * i;
  return result;
}

If n equals to -1, the program flow is interrupted and the following message is printed out:

Exception in thread "main" java.lang.IllegalArgumentException: N cannot be negative!
    at Example.factor(Example.java:7)
    at Example.main(Example.java:3)
Session 11
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment