Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 problem using custom exception handling in python
#1
I am using custom exception in my program along with built in exceptions given by python.
My code is:
class Error(Exception):
      
       pass

class ValueTooSmallError(Error):
      
       pass
class ValueTooLargeError(Error):
       
       pass

# user guesses a number until he/she gets it right
# you need to guess this number
number = 10
while True:
    try:
        i_num = int(input("Enter a number between 1-10: "))
        if i_num < number:
            raise ValueTooSmallError
        elif i_num > number:
            raise ValueTooLargeError
        break
    except ValueTooSmallError:
        print("This value is too small, try again!")
        print()
    except ValueTooLargeError:
        print("This value is too large, try again!")
        print()
print("Congratulations! You guessed it correctly.")
here I want that if user by mistakes enters a string instead of number, it should give ValueError that is the built-in exception in python...how do i use it with custom defined error...I did try but not getting it...Thanx in advance
Quote
#2
Whats wrong with ValueError? Why do you need to define custom errors?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Life of Brian: Conjugate the verb, "to go" !
Quote
#3
Add a except ValueError: to go along with the other exceptions.

Note: if i_num < number: is checking the entered number is less 10, it should be checking for less than 1 based on the input requirements.
Quote
#4
Why do you even need exceptions? Why not just put the code from the except blocks back into the conditional blocks that raise the errors in the first place?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Problem Discord Bot Python Kint 4 182 Aug-11-2019, 04:02 PM
Last Post: Kint
  Default setting for custom name input characteristics? beardelune 2 135 Aug-09-2019, 06:24 PM
Last Post: beardelune
  Handling Thick client applications using Python PraveenSubramaniyan 1 125 Jul-22-2019, 01:08 PM
Last Post: DeaD_EyE
  Looking for a "custom" script editor NewBeginnings 12 353 Jul-21-2019, 03:57 PM
Last Post: snippsat
  custom sqrt bluefrog 4 217 Jul-14-2019, 08:49 PM
Last Post: ichabod801
  How to fix 'uncaught exception of type NSException' in Python MonsterPython 0 121 Jul-09-2019, 06:52 AM
Last Post: MonsterPython
  Beginner problem in python script Cedmo 3 235 Jul-04-2019, 08:22 PM
Last Post: Cedmo
  Custom encoding problems. SheeppOSU 0 130 Jun-23-2019, 11:19 PM
Last Post: SheeppOSU
  Python problem reading file Lafayette 0 139 Jun-13-2019, 09:04 PM
Last Post: Lafayette
  'Exception Has occured: UnBoundLocalError' caston 1 168 Jun-12-2019, 02:33 PM
Last Post: perfringo

Forum Jump:


Users browsing this thread: 1 Guest(s)