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

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
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
  Handling Large XML Files (>10GB) in Python onlydibs 1 145 Dec-22-2019, 05:46 AM
Last Post: Clunk_Head
  Handling exception from a module dchi2 11 470 Nov-25-2019, 08:47 AM
Last Post: dchi2
  Importing Custom Modules in Python 3 Flexico 1 356 Aug-24-2019, 08:11 PM
Last Post: snippsat
  Handling Thick client applications using Python PraveenSubramaniyan 1 443 Jul-22-2019, 01:08 PM
Last Post: DeaD_EyE
  How to fix 'uncaught exception of type NSException' in Python MonsterPython 0 292 Jul-09-2019, 06:52 AM
Last Post: MonsterPython
  an easy way to disable exception handling Skaperen 6 680 Jun-02-2019, 10:38 PM
Last Post: Gribouillis
  exception handling KyawMyo 3 459 May-07-2019, 07:53 AM
Last Post: buran
  Handling pound sign (#) within custom URL chisox721 5 786 Apr-02-2019, 10:01 PM
Last Post: chisox721
  Warning / Error handling in python Prarthana_12 1 1,097 Feb-08-2019, 09:21 PM
Last Post: snippsat
  Database operation exception handling LostInCode 1 505 Jan-03-2019, 07:50 PM
Last Post: jeanMichelBain

Forum Jump:


Users browsing this thread: 1 Guest(s)