Python Forum
create my exception to my function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
create my exception to my function
#1
Hello,
I have a function that read data after a I split the message , and sometime I get wrong message-- which lead to wrong data input
I want to add to it exception , so in the main code I will see this problem and do what I want to do with this information

def splitingData(msg):
    data = msg.split('!')
    lng = len(data)
    i = 0
    for d in data:
        print(str(i) + ' : ' + d)
        i += 1
    if lng < 10:
        raise Exception('Missing data!')  ----> is this correct? 
    else:
        pass

main():
    try:
        splitingData(MyTestData)
    exception (what I need to do here?
Reply
#2
Try this for example
# normal function don't print anything
# when they do, take a name that indicates this.
def print_split_data(msg):
    data = msg.split('!')
    lng = len(data)
    i = 0
    for d in data:
        print(str(i) + ' : ' + d)
        i += 1
    if lng < 10:
        raise ValueError('Missing data in message')

def main():
    test_data = 'spam!eggs!ham!ham'
    try:
        print_split_data(test_data)
    except ValueError:
        print('Well the call to print_split_data() failed')

if __name__ == '__main__':
    main()
Output:
0 : spam 1 : eggs 2 : ham 3 : ham Well the call to print_split_data() failed
Reply
#3
Ok
this is also good for me

Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug New to coding, Using the zip() function to create Diret and getting weird results Shagamatula 6 1,456 Apr-09-2023, 02:35 PM
Last Post: Shagamatula
  python create function validation mg24 1 848 Nov-15-2022, 01:57 AM
Last Post: deanhystad
  Create a function for writing to SQL data to csv mg24 4 1,179 Oct-01-2022, 04:30 AM
Last Post: mg24
  Create SQL connection function and validate mg24 1 958 Sep-30-2022, 07:45 PM
Last Post: deanhystad
  TicTacToe Game Add Exception Handling and Warning Function ShaikhShaikh 5 2,430 Nov-03-2021, 05:02 PM
Last Post: deanhystad
  How to define a function to create a resorted list? sparkt 6 2,848 Aug-08-2020, 04:10 PM
Last Post: sparkt
  How to make this function general to create binary numbers? (many nested for loops) dospina 4 4,444 Jun-24-2020, 04:05 AM
Last Post: deanhystad
  Tried to create a function in a Datacamp course - why a is not equal to x_copy? danlin123 1 1,744 Jun-21-2020, 09:40 PM
Last Post: jefsummers
  Create exception handler oradba4u 7 3,117 May-23-2020, 03:25 AM
Last Post: oradba4u
  create function let_to_num() al_Czervik 2 2,130 Apr-17-2020, 10:44 PM
Last Post: al_Czervik

Forum Jump:

User Panel Messages

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