Exception handling added in retrieve function