Can I use a raised exception as an answer?

Can I use a raised exception as an answer?

de către Johan van Niekerk-
Număr de răspunsuri: 2

Hi,


I am setting python 3 tests and am trying to get my students to raise a specific exception in response to incorrect user input. For example:

                raise Exception("The value must be greater than 0")


Unfortunately, even though the output generated by running code matches the expected output exactly, it is seen as an error, and thus not marked. 

Any idea on what I should do to accept a specific exception as the correct answer?

Example of problem screenshot:

example screenshot

Etichete:
Ca răspuns la Johan van Niekerk

Re: Can I use a raised exception as an answer?

de către Johan van Niekerk-

Apologies for answering my own question. I thought it would be better to add the solution than to delete, just in case someone needs to do the same in future.

The solution is to catch the error in the input field.

try:

    x = DataPoint("ABCDEF", 23)

except Exception as e:

    print(str(e))


Now the output matches the desired raised error message exactly.

Ca răspuns la Johan van Niekerk

Re: Can I use a raised exception as an answer?

de către Richard Lobb-

Good answer :)

However, one thing to be aware of is that some students will simply print the expected output rather than throwing an exception. The easiest fix is to have a second similar test in which the exception handler prints something different.