0

I am working on a deduct amount function and it should raise a run time error if a < b in value:

here is my code

def deduct_amount(a, b):
    try:
        b - a < 0
    except ValueError:
        print(a + ' can not be less than' + b)
    else:
        c = a - b
        return c


deduct_amount(8, 12)


I know my try statement is faulty. how can I throw a value error if a is less than b

Ada_lovelace
  • 637
  • 2
  • 6
  • 18
  • 1
    You don’t need a try block to raise an error. See https://stackoverflow.com/q/4393268/217324 – Nathan Hughes Oct 10 '21 at 03:29
  • 1
    Does this answer your question? [Manually raising (throwing) an exception in Python](https://stackoverflow.com/questions/2052390/manually-raising-throwing-an-exception-in-python) – rustyhu Oct 10 '21 at 03:44

1 Answers1

6

A try/except block is to catch an exception. You want to raise an exception:

if a > b:
   raise ValueError("a must be less than b")
Jab
  • 26,853
  • 21
  • 75
  • 114