Python Forum
python multiple try except block in my code -- can we shorten code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python multiple try except block in my code -- can we shorten code
#5
I think you mean procedural programming. Functional programming is something completely different.

I think rob101 is biased against exceptions. Probably comes from learning programming in a language where exceptions are difficult to use. Having written millions of lines of code both ways I think the code is always cleaner using exceptions in place of exhaustive testing.

That said, this is not the way to write exception handlers.
    try:
        backup(sourcepath,destinationpath)
    except Exception as e:
        print("Error occured while taking backup")
        logger.info("Error occured while taking backup")
        sys.exit()
If the only possible recovery is exiting the program, one exception handler that catches them all is adequate. The only modification's I would make to Gribouillis' code is to removing the comment on raise so I don't have to open a log file to see the error trace, and he already predicted that.

Exceptions where you can recover aren't handled all the differently. It is mostly a matter of locale. Dangerous code is wrapped by try/except. You might have multiple handlers for multiple exception types, but they should all perform some corrective action that lets you move on with execution of the program.
calculator = {
    "+": lambda a, b: a + b,
    "-": lambda a, b: a - b,
    "*": lambda a, b: a * b,
    "/": lambda a, b: a / b,
}

while True:
    try:
        equation = input("Enter equation ")
        a, op, b = equation.split()
        print(equation, "=", calculator[op](float(a), float(b)))
    except KeyError:
        print("Unrecognized operator:", equation)
    except ZeroDivisionError:
        print("Cannot divide by zero")
    except ValueError:
        print("""Equation format is "number opeator number".""")
        print("""Supported opeerators are +, -, *, /.""")
Reply


Messages In This Thread
RE: python multiple try except block in my code -- can we shorten code - by deanhystad - Nov-08-2022, 10:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Equivalent Python code from VBA Mishal0488 4 1,068 May-02-2024, 10:17 PM
Last Post: DeaD_EyE
  Problem Converting Tradingview Indicator to Python code kralxs 1 334 Apr-27-2024, 06:10 PM
Last Post: kralxs
  Why can I not see the code correctly in Python IDLE.? Trump 8 827 Apr-04-2024, 07:47 AM
Last Post: jonesphedra
Sad Selenium update broke python code genericusername12414 1 302 Mar-16-2024, 07:33 PM
Last Post: snippsat
  Algorithm for extracting comments from Python source code Pavel1982 6 680 Feb-28-2024, 09:52 PM
Last Post: Pavel1982
  Python best library for Excel reports & review of existing code MasterOfDestr 4 858 Feb-14-2024, 03:39 PM
Last Post: MasterOfDestr
Lightbulb python code debuging yunus 1 387 Feb-11-2024, 03:48 PM
Last Post: deanhystad
  Python code to set column width 1418 11 1,548 Jan-20-2024, 07:20 AM
Last Post: Pedroski55
  Python code for alignment and font size 1418 0 367 Jan-14-2024, 03:56 AM
Last Post: 1418
  python convert multiple files to multiple lists MCL169 6 1,681 Nov-25-2023, 05:31 AM
Last Post: Iqratech

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020