7.2. Exercise 7.2

The following functions invoked in alloc_and_init() raise the following exception types if they should fail:

					allocate_array() noMem
					sort_array()     int
					register_data()  string
				

Insert one or more try blocks and associated catch clauses where appropriate to handle these exceptions. Simply print the occurrence of the error within the catch clause.

Rather than surround each individual function invocation with a separate try block, I have chosen to surround the entire set of calls with a single try block that contains three associated catch clauses:

 int *alloc_and_init( string file_name ) { ifstream infile( file_name.c_str() ); if ( ! infile )return 0; int elem_cnt; infile >> elem_cnt; if ( ! infile ) return 0; try { int *pi = allocate_array( ...

Get Essential C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.