usage of try except for review.

Ganesh Pal ganesh1pal at gmail.com
Mon Feb 29 12:34:44 EST 2016


On Mon, Feb 29, 2016 at 10:10 PM, Dennis Lee Bieber
<wlfraed at ix.netcom.com> wrote:

>         Ask yourself: Will my program still work if I remove all the assert
> statements. If the answer is "No", then you should not be using an assert.

You meant if the answer is "NO" then I should be using asset ?

>         Can your "run()" raise an exception? Since you never show it to us we
> can't tell. And if it can, which ones? However, checking a return code is
> not an exceptional condition -- that's expected logic.
>

The run api is like a wrapper to sub-process module it does a Popen
run's the command and returns stdout ,err and ret. The only exception
it can raise is a timeout error.

>>        try:
>>            if not run_cmd_and_verify(cmd, timeout=3600):
>
>         Since your version of rcav() always trapped exceptions internally, this
> call will never raise an exception, so using a try: block is meaningless
>>                return False
>
>         And your conditional basically comes down to:
>
>         if False:
>                 return False:
>
>
>>        except:
>>           logging.error("Some meaningful message")
>>    logging.info("Setup and Creation ....Done !!!")
>
>         But you fall off and return Null on success...

Iam using the try expect block to loop over  the list do I have
alternatives  but would prefer the for loop because in the actual
program there are 100 of command and putting them in a list is quite
easy and running over with the same operation saves many lines of code
,  Cam I modify it something like a try except with pass in the except
? or any suggestions


    for cmd in ["mount /nfs_mount1", "mount /cifs_mount1"]:
        try:
            if not run_cmd_and_verify(cmd, timeout=3600):
                return False
        except:
                pass
    logging.info("Setup and Creation ....Done !!!")

Regards,
Ganesh


More information about the Python-list mailing list