Python Forum
Exception not thrown in python3
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exception not thrown in python3
#1
Why does the call of inclusive_range() at line 71 not throw an exception and instead it prints "Test FAILED"?

def test(*args):
    print(type(args))
    print(len(args))
    if len(args) == 1:
        raise ValueError("too many args, [1, 2]")

def inclusive_range(*args):
    numargs = len(args)
    print(numargs)
    if numargs < 1:
        raise ValueError("number of args less than 1, needed interval [1, 3]")
    elif numargs == 1:
        start = 0
        stop = args[0]
        step = 1
    elif numargs == 2:
        (start, stop) = args
        step = 1
    elif numargs == 3:
        (start, stop, step) = args
    else:
        raise ValueError("number of args greater than 3, needed interval [1, 3]")
    i = start
    while i <= stop:
        yield i
        i += step

# test cases
def main():
    # valid 1 arg
    for i in inclusive_range(20):
        print(i, end=' ')
    print()
        
    # valid 2 args
    for i in inclusive_range(0, 21):
        print(i, end=' ')
    print()
    
    # valid 3 args
    for i in inclusive_range(0, 22, 2):
        print(i, end=' ')
    print()
    
    # invalid 0 args
    try:
        myRange = inclusive_range()
        for i in myRange:
            print(i)
    except ValueError as e:
        print(e)
    except:
        print("Unknown exception occured")
    else:
        print("Test FAILED")
        
    # invalid 4 args
    try:
        myRange = inclusive_range(1, 2, 3, 4)
        for i in myRange:
            print(i)
    except ValueError as e:
        print(e)
    except:
        print("Unknown exception occured")
    else:
        print("Test FAILED")
        
    # invalid 5 args, warning myRange not used (possibly optimized out by python)
    try:
        myRange = inclusive_range(1, 2, 3, 4, 5)
    except ValueError as e:
        print(e)
    except:
        print("Unknown exception occured")
    else:
        print("Test FAILED")
    
    test()
    try:
        test(1)
    except ValueError as e:
        print(e)
    else:
        print("everything is fine here, move along.")


if __name__ == "__main__": main()
Reply


Messages In This Thread
Exception not thrown in python3 - by ONEoo7 - Apr-08-2017, 08:34 PM
RE: Exception not thrown in python3 - by wavic - Apr-08-2017, 09:13 PM
RE: Exception not thrown in python3 - by Larz60+ - Apr-08-2017, 09:23 PM
RE: Exception not thrown in python3 - by ONEoo7 - Apr-08-2017, 09:24 PM
RE: Exception not thrown in python3 - by wavic - Apr-08-2017, 09:26 PM
RE: Exception not thrown in python3 - by zivoni - Apr-08-2017, 09:36 PM
RE: Exception not thrown in python3 - by ONEoo7 - Apr-08-2017, 09:45 PM
RE: Exception not thrown in python3 - by snippsat - Apr-09-2017, 01:58 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  which exception should be thrown for a sequence of the wrong length? Skaperen 1 876 Jan-06-2023, 04:13 AM
Last Post: deanhystad
  Pytest API Post call thrown errors pyseeker 4 3,681 Dec-08-2019, 04:53 PM
Last Post: pyseeker
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 4,978 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE
  Invalid argument error thrown. pyseeker 4 8,651 Sep-10-2019, 07:03 PM
Last Post: pyseeker
  During handling of the above exception, another exception occurred Skaperen 7 26,974 Dec-21-2018, 10:58 AM
Last Post: Gribouillis
  pytest fixture in conftest.py thrown error while in the test file runs OzzieOzzum 1 4,010 Jul-31-2018, 12:12 PM
Last Post: OzzieOzzum
  [split] Teacher (thrown in at the deep end - help) Mr90 2 3,037 May-23-2018, 02:04 PM
Last Post: DeaD_EyE
  Teacher (thrown in at the deep end - help) Mr90 5 3,946 May-22-2018, 01:08 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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