0

enter image description here

My exception handling does not seem to be working correctly. When I enter a string as input, the correct exception does not come up. When I enter a negative number, I have the same issue. Does anyone know how to fix this?

I tried to change the conditions that raise exceptions, but still did not find a solution to the issue.

def main(): try: height = int(input()) check_height = type(height) if height == 0 or height > 999: raise Exception elif check_height != int: raise TypeError elif height < 0: raise ValueError else: for blocks in range(height): if blocks == 0: print ("+-+") print ("| |") print ("+-+", end = "") else: print ("-+") for num in range(blocks): print(" ", end = "") print ("| |") for num in range(blocks): print(" ", end = "") print ("+-+", end = "") print("\n") except Exception: print("Value can't be equal to 0 or greater than 999") except TypeError: print("Value is not an integer") except ValueError: print("Value is less than 0") finally: pass main() 

The expected output should be a block that looks like this if the input entered was 1: (see screenshot of output above)

2
  • This doesn't make sense. What string? And if the call to int succeeds, how could type(thing) != int? Why isn't that the expected output? Commented Oct 13, 2019 at 21:37
  • @jonrsharpe Let me be more clear. Any data type other than int entered should cause a TypeError, which should be handled by the program. Commented Oct 13, 2019 at 23:26

1 Answer 1

1

Your exception handling had issues. Since you are trying to convert the input itself to integer, so it would throw ValueError right there if the input can not be converted to integer. And you did not have any ValueError exception handling, so it was going to the default exception block. Try this way:

try: height = int(input()) # check_height = type(height) if height <= 0 or height > 999: raise Exception # elif check_height != int: # raise TypeError # elif height < 0: # raise ValueError else: for blocks in range(height): if blocks == 0: print ("+-+") print ("| |") print ("+-+", end = "") else: print ("-+") for num in range(blocks): print(" ", end = "") print ("| |") for num in range(blocks): print(" ", end = "") print ("+-+", end = "") print("\n") except ValueError: print("Value is not an integer") # except TypeError: # print("Value is not an integer") except Exception: print("Value can't be less than 1 or greater than 999") finally: pass 
Sign up to request clarification or add additional context in comments.

8 Comments

This does not handle all the exceptions. These are the exceptions I need to handle: -A custom exception for the value being out of the 0 to 1000 value range. (I.e. if the input is equal to 0 or greater than 999) - In TypeError for the value not being an integer - ValueError if the user enters a value of less than 0 (I.e. negative numbers).
All the 3 scenario are being handled by the above code. I just interpreted 'Value out of range with respect to: 1-999' and 'Value less than 0' as same scenario. Do you have need to flag separate exception message when value provided is negative integer?
Yes, because right now when I enter a data type other than int, such as a string, it does not raise the error I am trying to raise which is a TypeError
height = int(input()) will cause a 'ValueError' when you are giving a string as an input, and not 'TypeError'. So, if you had been expecting the control to go under 'TypeError' block for that scenario, that is where you had a misunderstanding. Please execute this code for each of the scenario you have, and check if it is providing you proper error message that you expect under each scenario. And if you need a separate exception message when the user provides a negative integer (which should be under value out of range scenario anyways), then that can be handled as well.
Updated the message to "Value can't be less than 1 or greater than 999"
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.