Python Forum
problem using custom exception handling in python
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
Reply
#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.
Reply
#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.
Reply
#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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  custom exception dcr 1 238 Feb-17-2024, 08:19 PM
Last Post: Gribouillis
  I want to create custom charts in Python. js1152410 1 501 Nov-13-2023, 05:45 PM
Last Post: gulshan212
Question log.exception() without arguments in old Python versions? cthart 5 1,117 Nov-19-2022, 07:09 PM
Last Post: Gribouillis
Star python exception handling handling .... with traceback mg24 3 1,216 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  Handling pdf files with python. fuzzin 1 1,217 Jan-19-2022, 02:24 PM
Last Post: ThiefOfTime
  TicTacToe Game Add Exception Handling and Warning Function ShaikhShaikh 5 2,376 Nov-03-2021, 05:02 PM
Last Post: deanhystad
  Handling Python Fatal Error richajain1785 7 5,762 Oct-14-2021, 01:34 PM
Last Post: Tails86
  Problem with pexpect.exception.TimeOUT korenron 0 3,242 Apr-12-2021, 03:25 PM
Last Post: korenron
  Handling multi-input/output audio in python bor1904 4 3,496 Nov-04-2020, 08:25 AM
Last Post: CHLOVRL
  looking for a custom exception JarredAwesome 5 3,164 Sep-21-2020, 06:38 PM
Last Post: JarredAwesome

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020