Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Exception arguments? [duplicate]

I was trying to find a way to do this in python 3.6.5 which is not supported

try:
     c=1/0
     print (c)
except ZeroDivisionError, args:
     print('error dividing by zero', args)

It says this type of syntax is not supported by python 3.6.5 So is there a way to get the arguments of the exception?

like image 916
Arn121654 Avatar asked Oct 19 '25 02:10

Arn121654


1 Answers

How about:

try:
     c=1/0
     print (c)
except ZeroDivisionError as e:
     print('error dividing by zero: ' + str(e.args))

Comma notation is now used to except multiple types of exceptions, and they need to be in parentheses, like:

try:
    c = int("hello")
    c = 1 / 0
    print(c)
except (ZeroDivisionError, ValueError) as e:
    print('error: ' + str(e.args))
like image 184
Rob Bricheno Avatar answered Oct 21 '25 17:10

Rob Bricheno



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!