Frappe.throw and frappe.msgprint not working

I am trying to display some message to user if there is any error while getting the data through API by using either frappe.throw or frappe.msgprint.In some cases it is working perfectly fine,but in some other cases it is not working as I expected. Here is the code that I have tried so far.

    def get_context(context):
frappe.msgprint('Block and Level already exist')
if frappe.session.user == 'Guest':
        _('You need to be logged in to access this page'), frappe.PermissionError) #<-------- This is perfectly working
query_string = frappe.request.args
customer_id = None
    customer_id = query_string['customer_id']
except KeyError as e:        
    frappe.msgprint(  #<----------- Not working
        msg='Customer id not found',
    frappe.throw(_('Customer id not found')) # <------- Not working
return context

If I supply a value frappe.PermissionError to the parameter raise_exception then the method will work perfectly.