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
#11
Since Python 3.11 we got Zero Cost Exceptions.
If no exception is raised, there is nearly no loss in speed.
Only if an exception raises, it's slower.

It's ok to use try... except. Catching Exceptions too early is sometimes a problem.
It depends on the program itself and its organization.
+ it is OK to use Exceptions for control flow. Python is doing it aswell.

Fun fact 1: The control-flow of for-loops is realized with StopIteration. The iterator raises the StopIteration, if the iterator is exhausted.
Fun fact 2: A generator returns its result with a StopIteration and the result is stored in exception.args[0].
Larz60+ and Gribouillis like this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Code Help - pip install PyMuPDF python-docx pillow Splishsplash92 0 45 5 hours ago
Last Post: Splishsplash92
  python code to calculate mean of an array of numbers using numpy viren 3 163 May-29-2024, 04:49 PM
Last Post: Gribouillis
  Equivalent Python code from VBA Mishal0488 4 1,113 May-02-2024, 10:17 PM
Last Post: DeaD_EyE
  Problem Converting Tradingview Indicator to Python code kralxs 1 395 Apr-27-2024, 06:10 PM
Last Post: kralxs
  Why can I not see the code correctly in Python IDLE.? Trump 8 877 Apr-04-2024, 07:47 AM
Last Post: jonesphedra
Sad Selenium update broke python code genericusername12414 1 341 Mar-16-2024, 07:33 PM
Last Post: snippsat
  Algorithm for extracting comments from Python source code Pavel1982 6 738 Feb-28-2024, 09:52 PM
Last Post: Pavel1982
  Python best library for Excel reports & review of existing code MasterOfDestr 4 978 Feb-14-2024, 03:39 PM
Last Post: MasterOfDestr
Lightbulb python code debuging yunus 1 411 Feb-11-2024, 03:48 PM
Last Post: deanhystad
  Python code to set column width 1418 11 1,676 Jan-20-2024, 07:20 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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