0
global r
global t

def divide(a,b):
    try:
        c=a/b
        print(c)
        return c
    except:
       b+=1000
       r=b
       raise Exception

def main():
    try:
        r=0
        t=1000
        divide(t,r)
    except:
        print('except block')
        divide(t,r)

main()

How do i change the value of the variable r and then call the function again from the except block, after catching it as an exception?

1 Answers1

2

Don't do anything in divide. It's not its job to fix bad input parameters, nor catch the resulting exception. The caller can do those things.

def divide(a,b):
    c=a/b
    print(c)
    return c

Make the correction in main instead:

def main():
    try:
        r=0
        t=1000
        divide(t,r)
    except ZeroDivisionError:
        print('except block')
        r+=1000
        divide(t,r)
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • this is just an example where the error is caused by the variable r because its zero, but what if after incrementing r the exception might occur and needs to be incremented again then the divide function must be called – Shricharan Arumugam May 25 '18 at 12:27
  • @ShricharanArumugam Then you need a loop, just like in *any* situation where you want to do the same thing multiple times. – chepner May 25 '18 at 13:27