Client Library Switch Error Stack Code Example
Author: Quincey Koziol

### Client Library Desires to Switch Error Stack Code Example:

(Switch Error Stack Use Case Scenario)

    /* Client error class ID */
    hid_t CLIENT_ERROR;

    /* Major error code IDs */
    hid_t CLIENT_ERR_MAJOR_IO;		/* Errors on I/O */
       .
       .
       .

    /* Minor error code IDs */
    hid_t CLIENT_ERR_MINOR_OPEN;	/* Error during open */
    hid_t CLIENT_ERR_MINOR_WRITE;	/* Error during write */
       .
       .
       .

    int Client_do_write
    {
            const char *dset_name="a_dataset";
            hid_t      error_stack;

    		.
    		.
    		.

    	/* Make call to HDF5 I/O routine */
    	if(H5Dopen(file_id, dset_name)<0)
    	{
    		/* Push client error onto error stack */
    		H5Epush(H5E_DEFAULT,__FILE__,FUNC,__LINE__,CLIENT_ERR_MAJ_IO,CLIENT_ERR_MINOR_OPEN,"H5Dopen failed");

    		/* Indicate error occurred in function */
    		return(0);
    	}

    	/* Make call to HDF5 I/O routine */
    	/* (This shows client library preserves the current error stack before it's cleared by another HDF5 function) */
    	if(H5Dwrite(dset_id, mem_type_id, mem_space_id, file_space_id, dset_xfer_plist_id, buf)<0)
    	{
    		/* Push client error onto error stack */
    		H5Epush(H5E_DEFAULT,__FILE__,FUNC,__LINE__,CLIENT_ERR_MAJ_IO,CLIENT_ERR_MINOR_HDF5,"H5Dwrite failed");

                    /* Preserve the error stack by assigning an object handle to it */
                    error_stack = H5Eget_current_stack();

                    /* Close dataset */
                    H5Dclose(dset_id);

                    /* Replace the current error stack with the preserved one */
                    H5Eset_current_stack(error_stack);
                            .
                            .
                            .

    		/* Indicate error occurred in function */
    		return(0);
    	}
    		.
    		.
    		.

    	/* Successful return from function */
    	return(1);
    }