0

I have two functions. function "main" is calling to function "submain". "submain" function contains try-catch block and return some value.

If "submain" executes successfully then it returns some value which i will display to end user directly.

If i get an exception then i have to prepare one user friendly message and print that. But to do that i need to know whether returned value is exception or not.

How can i check that returned value in exception or valid result?

Here is my pseudo code:
def submain():
    try:
        result = call to external API
        return result
    excepion, e:
        _logger(e)

def main(value):
    for x in value:
        submain()
        # if return is exception then break loop and give msg to end user.
Ankit
  • 99
  • 1
  • 13
  • Would you please put your code? – Ghantey Sep 13 '18 at 05:18
  • Take a look at the section `Best Practices: except clause` in [this answer](https://stackoverflow.com/a/24065533/4134674). You can catch an exception at a lower level, do some required work and then re-raise it for the next upper level to catch and handle it again, if needed. – shmee Sep 13 '18 at 05:38
  • @markkeven: Added pseudo code. – Ankit Sep 13 '18 at 05:58
  • @Ankit 1) Pseudo code does not qualify as [MCVE](https://stackoverflow.com/help/mcve); please provide actual code. 2) Exceptions do not return, they raise. 3) Your requirement is pretty much covered by the mentioned section of the answer I linked. Please make the effort and try to understand it. Feel free to ask for clarification if needed. – shmee Sep 13 '18 at 12:47

1 Answers1

1

If you want to handle (try catch) the exception in function main then I think you should throw it from function submain with its context. If you swallow the exception in submain function there no way main function could know that without some tricks.

Anh Tran
  • 11
  • 1