-2

for example, on this code, there are two possibilities of value errors, delta can be negative or the variables can be strings. But i cant specify if its the delta error or the string variables error

import math


a = input('Input the value of A')
b = input('Input the value of B')
c = input('Input the value of  C')
delta = int(b) ** 2 - 4 * int(a) * int(c)

try:
    x1 = (int(-b) + math.sqrt(delta)) / 2 * int(a)
    x2 = (int(-b) - math.sqrt(delta)) / 2 * int(a)
    print('x1 is equal to : ', str(x1))
    print('x2 is equal to : ', str(x2))
except ValueError:
    print('Delta is negative, it is not possible to calculate X')
except ValueError:
    print('Variables must be numbers')

when i set the variables as strings i get this message

ValueError: invalid literal for int() with base 10: 's'
Raffaell
  • 5
  • 2
  • 1
    Exceptions are relatively expensive. It would be smarter for you to check for integers and non-negative values yourself before doing the computations, so you can diagnose things more precisely. As a general rule, exceptions should not be used to take the place of data validation. Exceptions are for unexpected errors -- exceptional conditions. – Tim Roberts Sep 17 '22 at 00:20
  • Please supply the full error message you're getting, including the full stack trace. The answer to your question depends on what line of code is producing that error. I will say that if you enter "s" for any one of your inputs, you're going to get that error, and that will have nothing to do with one kind of exception or another. I kinda see what you're trying to do with the ValueError. The problem there is that you'll hit that error when you calculate `delta`, and that line of code is not in your `try/except` structure. – CryptoFool Sep 17 '22 at 00:21
  • I kinda agree with @TimRoberts. For me, it's not so much that exceptions are expensive. For me, it's that you're using exceptions in a way that is very obscure and hard to follow. If you want to respond a certain way when `delta` is negative, then just check for that and respond accordingly. Using an exception the way you are will technically work, but it isn't the right way to go about what you're trying to do. – CryptoFool Sep 17 '22 at 00:24
  • This question is not a duplicate of typing, it is a duplicate of handling multiple occurances of a single exception. If marked as a duplicate is should at least be [a question lie this](https://stackoverflow.com/questions/10477634/except-statement-with-same-exception-class-as-parameter-twice) – thebadgateway Sep 17 '22 at 01:17

1 Answers1

-1

Do you mean something like this?:

from sys import exc_info
# https://docs.python.org/3/library/sys.html

try:
    # raise …
except ValueError:
    if exc_info()[1].startswith("invalid literal for int"):
        # do one thing
    elif:
        # do something else

# or
try:
    # raise …
except ValueError as e:
    if e.value.startswith("invalid literal for int"):
        # …
    elif:
        # …
thebadgateway
  • 433
  • 1
  • 4
  • 7