Some books
seem to include exceptions as an afterthought:
often the whole topic is taught in the last chapter.
As with implementation inheritance,
my course gradually introduces the various aspects of exceptions:
-
Exceptions first need to be mentioned when discussing
the manipulation of Strings
and when values are read from the keyboard.
So the students first meet
the exceptions called
StringIndexOutOfBoundsException
and IOException.
A distinction can be made at this stage between unchecked exceptions
and checked exceptions.
So, the
throws clause for IOException on the main method is also explained.
-
When method declarations are introduced,
you can explain why a throws clause is needed with a method declaration
as well as the main method.
-
The lectures on control statements
can introduce the use of a try statement
to handle an exception,
e.g., to handle NumberFormatException.
-
The lectures on using files can introduce the need for a finally clause.
Although in Java it can be tempting to overuse exceptions,
in this course
it is recommended that they are only used for untoward situations.
For this reason,
the idea of introducing your own exceptions
is briefly only mentioned at the end of the course.
A good article on the topic is
Designing with exceptions
by Venners.
You will find it at
http://www.javaworld.com/javaworld/jw-07-1998/jw-07-techniques.html