Except block is not working in try except in python

I am trying to get list of pods from specific namespace in aws lambda (python).

Its giving right and expected list of pods. But When I pass wrong namespace, it is not skipping the try block and not catching the except block to return the correct response. Any reason for this ?

`import json from kubernetes.client.rest import ApiException

def get_pods(namespace: str, apiCoreclient: object):

response_output = {}
response_output["statusCode"] = 200    

retList = []
items = apiCoreclient.list_namespaced_pod(namespace).items
for item in items:
    try:
        retList.append(item.metadata.name)
        response_output['Pods'] = json.dumps({"Pods": retList})

    except ApiException as e:
        response_output["error"] = e.reason
        response_output["statusCode"] = e.status

response_output["isBase64Encoded"] = "False"       
return json.dumps(response_output)`