Java Implementation of handling exceptions