Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Line number in Try Catch block?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Line number in Try Catch block?

    Hello There,

    Is there a way to get the source line number when catching exceptions?

    I suppose I could put a descrption when printing out the exception number ... but that would take som effort :-)

    Many Thanks, Caesar.

    #2
    Hello Caesar,

    Thank you for your inquiry.

    I'm not aware of a way to print the line number of the source of the exception. You may have to add some print statements intermittently throughout your code to see what was the last print before the exception was thrown in order to narrow down the source of the error, or you could comment out different portions of code at a time to see if this helps you to narrow it down. For more tips on debugging your scripts, see the page here:


    Please let us know if we may be of further assistance.

    Comment


      #3
      Many Thanks Emily!

      Comment


        #4
        Originally posted by Skechers View Post
        Hello There,

        Is there a way to get the source line number when catching exceptions?

        I suppose I could put a descrption when printing out the exception number ... but that would take som effort :-)

        Many Thanks, Caesar.

        Of course, try something like that. But you only get the line number if you switch on debug mode ;-)
        Code:
        private void TestMethod()
        {
             try
            {
                //do some stuff
             }
              catch (Exception ex)
             {
                 string errorMsg = ExceptionInfo(ex, MethodBase.GetCurrentMethod().DeclaringType.Name);
                 Print(errorMsg);
              }
        }
        
        private string ExceptionInfo(Exception exception, string name, [CallerMemberName] string callingMethod = "")
        {
           var stackFrame = new StackTrace(exception, true).GetFrame(0);
           return string.Format("{0} [{1}]-[{2}]-[{3}]-[Line={4}]-[Column={5}]-[FileName={6}]-{0} [Error={7}] {0}"
                    , Environment.NewLine, name, callingMethod, stackFrame.GetMethod(), stackFrame.GetFileLineNumber(), stackFrame.GetFileColumnNumber(), stackFrame.GetFileName(),       exception.ToString());
        }
        
        private void Print(string msg, [CallerMemberName] string callingMethod = "", [CallerLineNumber] int callingLineNumber = 0)
        {
             Output.Process(msg + "  " + callingMethod + "  " + callingLineNumber, PrintTo.OutputTab1);
        }
        sidlercom80
        NinjaTrader Ecosystem Vendor - Sidi Trading

        Comment


          #5
          Skechers If you compile by right-clicking on the script editor and turn on Debug mode, then the stack trace for your unhandled exception will automatically be put into your trace file in Documents -> NinjaTrader 8 -> trace.

          Without debug mode it's still in there, but without the line numbers or amount of detail about the call stack.
          Bruce DeVault
          QuantKey Trading Vendor Services
          NinjaTrader Ecosystem Vendor - QuantKey

          Comment


            #6
            Many Thanks Bruce & Slider!

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by CarlTrading, 03-31-2026, 09:41 PM
            1 response
            45 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by CarlTrading, 04-01-2026, 02:41 AM
            0 responses
            21 views
            0 likes
            Last Post CarlTrading  
            Started by CaptainJack, 03-31-2026, 11:44 PM
            0 responses
            31 views
            1 like
            Last Post CaptainJack  
            Started by CarlTrading, 03-30-2026, 11:51 AM
            0 responses
            50 views
            0 likes
            Last Post CarlTrading  
            Started by CarlTrading, 03-30-2026, 11:48 AM
            0 responses
            42 views
            0 likes
            Last Post CarlTrading  
            Working...
            X