robust classes

2 views
Skip to first unread message

Chi Ming

unread,
Apr 24, 2011, 8:44:55 PM4/24/11
to UBC CPSC 210
hi i was just looking at the old lectures and was wondering why are
there 2 constructors for the exceptions.
For trafficlightexception per se?

Paul Carter

unread,
Apr 24, 2011, 9:32:00 PM4/24/11
to ubc-cp...@googlegroups.com
Let's consider SequenceException.

The constructor that takes no parameters (i.e., the default constructor) will construct an exception with null as the error message (see the documentation for Exception). The second constructor (that takes a string as a parameter) allows us to specify the error message associated with the exception (again, see the documentation for Exception).

It's often useful to associate error messages that provide specific information about the problem. Suppose we are designing an exception that will be thrown if a client provides an index that is out of bounds when using the following method of the ArrayList<E> class, for example:

public E get( int index );

We could tailor the error message so that the incorrect index appears as part of that message - this can be very helpful for debugging purposes.

Paul

Reply all
Reply to author
Forward
0 new messages