Getting the exact error message

I am a newbie in Python, My algo threw an error today because of the strike I was trying to buy wasn't available. So I was buying 40300 CE for hedging and there was an error, however the exception caught was:
"local variable 'order_id' referenced before assignment "

I was checking this after market hours and had no clue what had gone wrong. It became only apparent when I ran this code manually the next day during the market hours. My question is: how to get the exact error message thrown by Zerodha Kiteconnect.
The error message when ran manually was: Order placement failed: The instrument you are placing an order for has either expired or does not exist.
Exception when printed, printed this:
"local variable 'order_id' referenced before assignment "

So I would like to get both the messages, how do I do that?

my code snippet:
except Exception as e:
print("Something went wrong:",e)

    SRIJAN edited August 2022
    The exception 'Order placement failed...' is from KiteConnect .

    The error,'local variable...' is an python error.
    This occurs inside a function.

    If you are using the try-except as you showed here in your program with the API call,you would have got the KiteConnect exception message too on the console.

    But,only the local python error would have been displayed under traceback.
  • dcoolsam
    Thanks for the reply Srijan. Yes, I am getting it on console, but was wondering if I could get in traceback. But looks like it is not possible. Thanks for replying.
    SRIJAN edited August 2022
    If you put it under try-except,you won't get it under traceback.

    That's the whole purpose of try-except,to not break the program,if an error occurs.

    Still,if you want the exception under traceback without breaking the program,you can do:

    from traceback import print_exc

    KiteConnect API call
Sign In or Register to comment.