Home > Enterprise >  How to write code to catch null exception in python
How to write code to catch null exception in python

Time:03-31

I have below code, which I want to write in a way so that it catches null exception in python if key is not available.

    def vars_load_summary(self, response):
        loadStartTime = datetime.strptime(response['Attributes']['loadStartTime'], '%Y-%m-%d %H:%M:%S')
        loadEndTime = datetime.strptime(response['Attributes']['loadEndTime'], '%Y-%m-%d %H:%M:%S')
        time_taken = loadEndTime - loadStartTime
        logger.info(f' load duration :::, {time_taken}')
        renditionBuilderStart = datetime.strptime(response['Attributes']['renditionBuilderStart'], '%Y-%m-%d %H:%M:%S')
        renditionBuilderEnd = datetime.strptime(response['Attributes']['renditionBuilderEnd'], '%Y-%m-%d %H:%M:%S')
        rendition_duration = renditionBuilderEnd - renditionBuilderStart
        logger.info(f' rendition duration :::, {rendition_duration}')
        indexBuilderStart = datetime.strptime(response['Attributes']['indexBuilderStart'], '%Y-%m-%d %H:%M:%S')
        indexBuilderEnd = datetime.strptime(response['Attributes']['indexBuilderEnd'], '%Y-%m-%d %H:%M:%S')
        index_duration = indexBuilderEnd - indexBuilderStart
        logger.info(f' index builder duration :::, {index_duration}')
        varsLoadStatus = response['Attributes']['loadStatus']
        logger.info(f' load duration :::, {LoadStatus}')

I mean if no loadStartTime or loadEndTime or renditionBuilderStart or renditionBuilderEnd time is not present . How to handle this scenario. I am new to python programming

CodePudding user response:

Maybe try this

if key is None:
    raise TypeError

or

if key is None:
    print("There is no key.")

CodePudding user response:

This is what I have trieD

    def vars_load_summary(self, response, loadStartTime, loadEndTime, renditionBuilderStart, renditionBuilderEnd, indexBuilderStart, indexBuilderEnd):
        key = (loadStartTime, loadEndTime, renditionBuilderStart, renditionBuilderEnd, indexBuilderStart, indexBuilderEnd)
        if key is None:
            return [{'Message': 'Key not found'}]
        else:
            loadStartTime = datetime.strptime(response['Attributes']['loadStartTime'], '%Y-%m-%d %H:%M:%S')
            loadEndTime = datetime.strptime(response['Attributes']['loadEndTime'], '%Y-%m-%d %H:%M:%S')
            time_taken = loadEndTime - loadStartTime
            logger.info(f' load duration :::, {time_taken}')
            renditionBuilderStart = datetime.strptime(response['Attributes']['renditionBuilderStart'], '%Y-%m-%d %H:%M:%S')
            renditionBuilderEnd = datetime.strptime(response['Attributes']['renditionBuilderEnd'], '%Y-%m-%d %H:%M:%S')
            rendition_duration = renditionBuilderEnd - renditionBuilderStart
            logger.info(f' rendition duration :::, {rendition_duration}')
            indexBuilderStart = datetime.strptime(response['Attributes']['indexBuilderStart'], '%Y-%m-%d %H:%M:%S')
            indexBuilderEnd = datetime.strptime(response['Attributes']['indexBuilderEnd'], '%Y-%m-%d %H:%M:%S')
            index_duration = indexBuilderEnd - indexBuilderStart
            logger.info(f' index builder duration :::, {index_duration}')
            varsLoadStatus = response['Attributes']['loadStatus']
            logger.info(f'load duration :::, {varsLoadStatus}')

  • Related