Question

In python is there a way to not return to the caller function if a certain event happened in the called function. For example,...

def acquire_image(sdkobject):

    ret = sdkobject.PrepareAcquisition()
    error_check(ret)

    ret = sdkobject.StartAcquisition()
    error_check(ret)

error_check is a function that checks the return code to see if the sdk call had an error. If it is an error message then I would like to not go back to acquire and image but go to another function to reinitalise the sdk and start from the beginning again. Is there a pythonic way of doing this?

Was it helpful?

Solution

Have your error_check function raise an exception (like SDKError) if there is a problem, then run all the commands in a while loop.

class SDKError(Exception):
    pass

# Perhaps define a separate exception for each possible
# error code, and make a dict that maps error codes to the
# appropriate exception.

class SDKType1Error(SDKError):
    pass

class SDKType5Error(SDKError):
    pass

sdk_errors = {
    1: SDKType1Error,
    5: SDKType5Error,
}

# Either return, if there was no error, or raise
# the appropriate exception
def error_check(return_code):
    if return_code == 0:
        return   # No error
    else:
        raise sdk_errors[return_code]


# Example of how to deal with specific SDKErrors subclasses, or a generic
# catch-all SDKError
def acquire_image(sdkobject):

    while True:
        try:
            # initialize sdk here
            error_check(sdkobject.PrepareAcquisition())    
            error_check(sdkobject.StartAcquisition())
        except SDKType1Error:
            # Special handling for this error
        except SDKError:
            pass
        else:
            break

OTHER TIPS

Return the error and use an if condition to check if the returned value has error, and if it has, call the reinitialization code from the calling function.

Use return for happy scenario

Returning to calling function is done by simple return or return response.

Use it for solving typical run of your code, when all goes well.

Throw exception, when something goes wrong

If something goes wrong, call raise Exception(). In many situations, your code does not has to do it explicitly, it throws the exception on its own.

You may even you your own Exception instances and use them to pass to the caller more information about what went wrong.

It took me a while to learn this approach and it made my coding much simpler and shorter then.

Do not care about what will your calling code do with it

Let your function do the task or fail, if there are problems.

Trying to think for client responsibility in your function will mess up your code and will not be complete solution anyway.

Things to avoid

Ignore who is calling you

In OOP this is principle of client anonymity. Just serve the request and do not care, who is calling.

Do not attempt using Exceptions as replacement for returning a value

Sometime, people use the fact, Exception can pass some information to to caller. But this is rather antipattern (there are always exception.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top