Adding more flexiblity to Example 21 and guarding against possible Exception