Defined the critical exception handling in a new function