Python Forum
Detecting float or int in a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Detecting float or int in a string
#6
Clunk_Head Wrote:Can you please provide an example where an exception must be used in place of anything else?
Exceptions free intermediary code from checking all the obstacles that can occur for completing a task. For example
def get_cup_of_coffee():
    go_to_recreation_room()
    open_the_door()
    go_to_coffee_machine()
    insert_coin()
    push_button()
    cup = take_coffee()
    return cup
Many things can go wrong. The door could be locked, the machine could be in maintenance, the coin could be rejected, etc. All these events, which are not errors should raise an exception when the corresponding function is called. But the logic of get_cup_of_coffee() does not need to handle these exceptions. It lets them propagate and they all result in get_cup_of_coffee() sending an exception.

Now the code that uses this function can freely decide to catch the exceptions or not, for example
def ten_o_clock_routine():
    try:
        coffee = get_cup_of_coffee()
    except RecreationRoomLocked:
        # take appropriate action
    except Exception:
        # other errors
    else:
        coffee.drink()
The main gain is to choose at which level we catch the exceptions in the code (if they are caught).

In a language without exceptions, such as C, get_cup_of_coffee() would have the responsibility to handle all the exceptional cases that can happen in the functions that it calls. In some other languages, we would need to declare all the exception types that are thrown by the function, which is tedious. But not in Python.
Reply


Messages In This Thread
Detecting float or int in a string - by Clunk_Head - May-23-2022, 08:39 PM
RE: Detecting float or int in a string - by Gribouillis - May-23-2022, 09:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python calculate float plus float is incorrect? sirocawa 6 464 Apr-16-2024, 01:45 PM
Last Post: DeaD_EyE
  convert string to float in list jacklee26 6 2,016 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  iterating and detecting the last Skaperen 3 1,140 Oct-01-2022, 05:23 AM
Last Post: Gribouillis
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,996 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Convert string to float problem vasik006 8 3,518 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  module detecting if imported vs not Skaperen 1 1,715 Nov-19-2021, 07:43 AM
Last Post: Yoriz
  detecting a generstor passed to a funtion Skaperen 9 3,755 Sep-23-2021, 01:29 AM
Last Post: Skaperen
  Python BLE Scanner not detecting device alexanderDennisEnviro500 0 2,054 Aug-01-2021, 02:29 AM
Last Post: alexanderDennisEnviro500
  Detecting power plug Narayan 2 2,775 Aug-01-2020, 04:29 AM
Last Post: bowlofred
  ValueError: could not convert string to float: RandomCoder 3 5,841 Jul-27-2020, 07:38 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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