On 02/08/18 15:29, Shall, Sydney wrote:

> uvc = 2
> msg = "Bad argument provided, the value of uvc must be a float."
> 
> try:
>      type(uvc) == float
> except TypeError as e:
>      print(e, msg)

The try block contains an expression that will always evaluate
to True or False and thus never raise an exception. Since the
expression is never stored or used(in an if or while say)
the block does nothing useful.

> try:
>      0.0 > uvc < 1.0
> except ValueError:
>      print("Bad argument provided. The value of uvc must be "
>        "greater than 0.0 and less than 1.0.")

This also has an expression but it will never generate a
ValueError since its only testing if uvc is between the
two values. Again it will either be True or False provided
the value of uvc is of a numeric type....

If not it will generate a TypeError.
So rather than testing for a ValueError which will
never happen, you should be testing for a TypeError.

Exceptions are about handling the unexpected, not about
managing flow control in your normal execution path.

> if type(uvc) != float:
>      raise TypeError("Bad argument provided. And this is also old test."
>                      " The value of UVC must be a float. This is old test")

Raising the error is fine but it will cause your
program to stop. Is that what you want?

But mostly in Python we don't check types before using them
we use TypeError to catch the mistake when it happens. we
are not using try/except to look for wrong types, we
just catch the error if it occurs. Mostly we hope it won't.

try:
   your expected normal code here
except SomeError
   catch anything that you hope won't happen but might.


> I am now rewriting a class, as recommended by both Alan and Steven, in 
> which I have previously used these formulations.
> 
> today. Either I do not need this change or I have written the try, 
> except incorrectly. I am sure that I am making an elementary mistake.

Maybe both.
If you really do need to do the checks as part of your normal
logic then continue using the if style.

But if you just think there's a possibility of a wrong value/type
being passed then write your code as if there were no errors
but wrap it in a try/except block. But only if you can actually do
something when the error occurs, otherwise you might as well let
Python deal with it and print a traceback.

The key thing about try/except is that you should be able to
delete it and your code (ie. the try block) should carry on
working albeit with an occasional traceback when an error
occurs. The traceback tells you which errors you can catch,
but they are only worth catching if you can do something
about them. (Which might include logging them or printing
a more friendly message before exiting)


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to