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].
Gribouillis and Larz60+ 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 not running Azdaghost 1 325 Apr-22-2025, 08:44 PM
Last Post: deanhystad
  Mathematica code vs Python MiloChocolatito 0 539 Mar-31-2025, 06:32 AM
Last Post: MiloChocolatito
  I am getting an IndentError on my python code in VS Code and i dont know why jcardenas1980 11 5,377 Mar-22-2025, 09:49 AM
Last Post: Pedroski55
  I trying to automate the Variable Logon button using the python code but I couldn't surendrasamudrala 0 340 Mar-07-2025, 05:02 AM
Last Post: surendrasamudrala
  How do i run python code? smattiko83 5 819 Mar-06-2025, 09:20 PM
Last Post: ArchieLinux
  I'm new to Python - can someone help with this code as it is not working? lminc123 1 585 Feb-13-2025, 06:13 PM
Last Post: lminc123
  If I need to do regex replacement 27000 times, how to shorten it? tatahuft 4 915 Dec-26-2024, 01:51 AM
Last Post: deanhystad
  I cannot create a virtual environment on visual studio code using python Willem_Aucamp316 2 4,091 Nov-27-2024, 02:20 PM
Last Post: menator01
  CLI to python code azxo1 11 2,354 Oct-21-2024, 08:32 AM
Last Post: azxo1
  Algorithm for extracting comments from Python source code Pavel1982 7 3,120 Aug-28-2024, 02:50 AM
Last Post: timothyferriss

Forum Jump:

User Panel Messages

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