-1

I have written Python code which does some calculation. During this it converts string to float. However sometimes numeric string value may be empty that time its giving me valueError. I tried to keep that in try catch block however its going to another exception block as shown below.

try:
   float(some value)
except Exception as ValueError:
   print(error message)

except Exception as oserror:
   print(mesage)

Its going to os error block instead of ValueError block

Vikas Periyadath
  • 3,088
  • 1
  • 21
  • 33
Pradeep Shanbhag
  • 437
  • 5
  • 8
  • 19
  • And what does the oserror say? – Plasma Feb 16 '18 at 12:31
  • Syntax: `except WhatToCatch:` or `except WhatToCatch as HowToNameItForReference:` - read https://docs.python.org/3/tutorial/errors.html - WhatToCatch: https://docs.python.org/3/library/exceptions.html#exception-hierarchy – Patrick Artner Feb 16 '18 at 12:37
  • Possible duplicate of [Try/Except in Python: How do you properly ignore Exceptions?](https://stackoverflow.com/questions/730764/try-except-in-python-how-do-you-properly-ignore-exceptions) – Patrick Artner Feb 16 '18 at 12:39

1 Answers1

5

That's not how you capture exceptions.

try:
    float(some value)
except ValueError as e:
    print("here's the message", e.args)
except OSError as e:
    print("here's a different message")

(Note, though, there's no instance when calling float would raise an OSError.)

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895