Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Exceptions to the programming rules, Part 1

Learn how exception handling has evolved from C to Java

  • Print
  • Feedback

Page 5 of 7

Stack object created. Stack holds a maximum of 5 integer values.
Attempting to push integer value 52.
Attempting to push integer value -32.
Attempting to push integer value 468.
Attempting to push integer value 345.
Attempting to push integer value 42.
Attempting to push integer value 91.
Stack is full. Could not push integer value 91. Top index equals 4.
Popping integer value 42.
Popping integer value 345.
Popping integer value 468.
Popping integer value -32.
Popping integer value 52.
Stack is empty. Could not pop integer value. Top index equals -1.


The main() function begins by creating a Stack object capable of storing a maximum of five integer values. main() then uses cout and the overloaded << operator to output some information to the standard output device. Following that activity, main() creates an array of six integer values -- and then does something unusual: main() enters a try block.

A try block consists of statements, prefixed by keyword try, that sandwich between open/close brace characters. The statements (including function call statements) within that block can potentially throw exception objects. By placing statements in a try block, a developer tells C++ that the program plans to catch exception objects in a catch clause that associates with the try block.

A close look at the first try block reveals an attempt to push six integer values onto the stack. Because the stack has room for only five integer values, it cannot store the sixth integer value. Look at the source code for the push() member function in the Stack class. That code includes throw Full (top, value);. When Stack's internal values array runs out of room, C++ creates an object from Stack's Full member class and initializes that object to the current contents of the top and value variables. That object subsequently gets thrown, via throw, to the runtime code.

The runtime code looks for a catch clause that can catch objects of class type Stack::Full. (That syntax identifies Full as a member of Stack -- an instance inner class, as it were.) The search begins in the function that contains the try block. Immediately following the try block is a catch (Stack::Full f) clause. The runtime code notes that clause as being able to catch objects of class type Stack::Full -- and passes execution to that clause. The clause prints a message and calls various member functions on the object (referenced by f) to obtain information previously stored by push()'s throw statement. That information consists of the top index and integer value pushing at the time of the exception. Apart from displaying those values, excdemo does nothing with them.

After the catch clause handles the exception, execution leaves that clause and enters the second try block. That block attempts to pop six integer values from the stack. However, an exception occurs during the sixth pop attempt because only five integer values are on the stack. That exception results in the creation of a Stack::Empty object (in Stack's pop() member function) that is thrown to the runtime code. The runtime code finds the catch (Stack::Empty e) clause (immediately after the try block) and passes execution to that clause -- because the clause's Stack::Empty parameter type matches the thrown object's type. Appropriate exception information then prints.

  • Print
  • Feedback

Resources