0

so my question is that in my code actually on consists of two python module on the main and other the sub. In sub there is a function which I need to stop the working and return the control back to the next line of the main module. How can it be done. I tried exit() but it stops the execution. just for example

#module 1 -- ex2

import ex1
def add():
 print('before calling ex1')
 ex1.sub()
 print('after calling ex1')
add()

#module 2 -- ex1

def sub():
 print('inside sub')
 error()
 print('after error call')
def error():
 print('inside error')
 return #in place of this

so I want is that when my code enters the error function it will print the statement and the module 2 ends execution and the control will flow to the print('after calling ex1') this statement in module 1. Please help guys

note: please do not use multiple returns as example:

#module 2 -- ex1

def sub():
 print('inside sub')
 error()
 return # Please do not use
 print('after error call')
def error():
 print('inside error')
 return #in place of this

I need the output to be like ---

before calling ex1
inside sub
inside error
after calling ex1 
Barmar
789k57 gold badges555 silver badges669 bronze badges
asked Jul 29, 2021 at 7:59
2
  • 1
    Use exceptions and try/except Commented Jul 29, 2021 at 8:05
  • 2
    Modules don't implement control flow, only functions do. A module is just a collection of functions/variables and a namespace. Commented Jul 29, 2021 at 8:06

2 Answers 2

2

This is what exception handling is designed for, with the raise statement and the try .. except construct in Python:

#module 1 -- ex2
import ex1
def add():
 print('before calling ex1')
 try:
 ex1.sub()
 except RuntimeError as e:
 print('An error occurred while calling ex1.sub:', str(e))
 # or just a pass statement if you do not want anything done about the error
 print('after calling ex1')
add()
#module 2 -- ex1
def sub():
 print('inside sub')
 error()
 print('after error call')
def error():
 print('inside error')
 raise RuntimeError('some error')
answered Jul 29, 2021 at 8:07
Sign up to request clarification or add additional context in comments.

2 Comments

This will technically print that an error occurred too, so perhaps you should replace that line with pass
@Bonfire Sure, added a note then.
0

Your error function must be a function that returns a value. True or False

def error():
 print('inside error')
 return True 
 # or return False
def sub():
 print('inside sub')
 if(error()):
 return
 print('after error call')
answered Jul 29, 2021 at 8:14

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.