-4

I want to know the side effects or unknown issues of using try/except block in the below approaches?

Approach 1:

def f1():
    try:    
        # some code here
    except Exception as e:
        print(str(e))
def f2():
    try:
        f1()
    except Exception as e:
        print(str(e))

Approach 2: Same logic as in approach 1 but without try/block in f1()

def f1():
    # some code here

def f2():
    try:
        f1()
    except Exception as e:
        print(str(e))

Approach 3: Using multiple nested functions

def f1():
    # some code here

def f4():
    # some code here

def f3():
    f4()
    # some code here

def f2():
    try:
        f1()
        f3()
    except Exception as e:
        print(str(e))

Approach 4: Adding multiple try/except in every function

def f1():
    try:
        # some code here
    except Exception as e:
        print(str(e))

def f4():
    try:
        # some code here
    except Exception as e:
        print(str(e))

def f3():
    try:
        f4()
        # some code here
    except Exception as e:
        print(str(e))

def f2():
    try:
        f1()
        f3()
    except Exception as e:
        print(str(e))
ashish14
  • 650
  • 1
  • 8
  • 20

2 Answers2

1

when using try/except failing silently is dangerous. In my opinion reusable functions don't have much context (information) on how the caller want to handle the exception, it is better to throw exception to be handled by caller.

If you are handling exception on reusable function then its behaviour should be predictable and consistent. Like may be you want to return fallback value, or may be you want to throw another error with enough context for user etc

So I will prefer approach 3 where caller handles the exceptions

Dev Khadka
  • 5,142
  • 4
  • 19
  • 33
1

Knowing where to catch exceptions in your code is an important design choice, and often depends on what exceptions you're expecting to catch.

There is the concept of exceptions 'bubbling up' - if an exception is raised in the inner function but there's no try block, the exception will be passed up to the function that called it. This continues until either an appropriate except statement is met and the exception is handled, or it reaches the top level and causes the program to terminate with a stack trace.

Choosing where to handle your exceptions therefore depends on what exceptions you're expecting your functions to throw and then handling these in the appropriate place in the code e.g. where you can return a meaningful message to the user and ask them to retry. There's nothing overtly wrong with any approaches you've defined, but it depends on what the functions raise and how you want to handle the exceptions.

Worth noting it's bad practice to except Exception as this will capture even keyboard interrupts (user pressing CTRL+C). Where possible you should handle the specific exceptions thrown by the function you've called (should be in the function docs) and take the appropriate action.

Useful background https://docs.python.org/3/tutorial/errors.html

elembie
  • 600
  • 2
  • 8