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 NullPointStrategies, Today, 05:17 AM
            0 responses
            53 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            130 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            70 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            44 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            49 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X