Added missing use statements and improved logic flow for exception messages