Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Identify Exception Line Number

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

    Identify Exception Line Number

    Is there a way to identify what line number caused an exception other than wrapping each individual line with a try/catch?

    If I do something like the following I only get the general error message. Best I can tell, the other members in the Exception object do not give the line number (as it does in Visual Studio):

    OnBarUpdate()
    {
    try
    {
    //stuff
    }
    catch(Exception e)
    {
    Print(e.Message)
    }
    }


    PS. A side note: A Programming Forum should not strip out text formatting such as leading spaces and tabs. It makes reading code examples very difficult.

    thanks,
    shawnj

    #2
    >> Is there a way to identify what line number caused an exception other than wrapping each individual line with a try/catch?
    None I'm aware of.

    Comment


      #3
      shawn

      Formatting is not stripped, you just have to surround your code with the proper tag.

      Code:
      OnBarUpdate()
      {
          try
          {
              //stuff
          }
          catch(Exception e)
          {
              Print(e.Message)
          }
      }
      If you're getting an exception in your code, you really only need to check for
      1. Divide by 0
      2. Access to a null object.

      Also, you want to be sure you're not referencing a bar number that doesn't exist. Try placing this line first in your OnBarUpdate() method.

      Code:
      if(CurrentBar < [I]whateveryourlongestperiodis)
      [/I]    return;
      to avoid any bar referencing problems.
      mrlogik
      NinjaTrader Ecosystem Vendor - Purelogik Trading

      Comment


        #4
        Second thought: I believe Exception.ToString() or Exception.StackTrace might hold the info you are looking for.

        Comment


          #5
          Originally posted by NinjaTrader_Dierk View Post
          Second thought: I believe Exception.ToString() or Exception.StackTrace might hold the info you are looking for.
          Thanks Dierk. I think e.Message is basically the same thing as Exception.ToString(). StackTrace holds promise but it does not give line number. I'm thinking in Visual Studio you have to run in debug mode to get the line number. I haven't tried (yet) to see if running NinjaTrader with debug mode enabled will give the line number. I don't think I can afford the cpu cost running realtime with debug mode enabled though.

          Comment


            #6
            Thanks mrlogik for the format code tip:

            Code:
            TestFormat()
            {
               //Space Works!
            //No tab, best I can tell. :(
            }
            I'm getting a very intermittent "index out of bounds" exception. I'm doing a bunch of index reads and writes to a container class.

            I think I Am going to have to wrap each line with a try/catch.
            Last edited by shawnj; 12-03-2008, 02:57 PM.

            Comment


              #7
              Any time you reference an object / array / whatever... anything with a bound you should check to make sure you don't go out of bounds.

              is it on a DataSeries? make sure your period (how far back you go) is not greater than the # of bars loaded on the chart.
              mrlogik
              NinjaTrader Ecosystem Vendor - Purelogik Trading

              Comment


                #8
                Thanks mrlogik. I'm using a List<MyClass>.

                Of course I could wrap each access of the List with something like:

                if ( (index >= 0) && (index < MyList.Count) )
                {
                MyList[index].DoSomething;
                }

                But as I've written my logic, I don't think index should ever be less than 0 or greater than Count-1. So I would like to understand better what is going in my code to very intermittently cause it.

                Well, I guess I could just do something like the following:

                if ( (index >= 0) && (index < MyList.Count) )
                {
                MyList[index].DoSomething;
                }
                else
                {
                Print("Index error at this location. Dump of members: ");
                }

                But then it might be better to just write:

                try
                {
                MyList[index].DoSomething;
                }
                catch(Exception e)
                {
                Print("Exception at this location. Dump of members: ");
                Print(e.Message);
                }

                Comment


                  #9
                  For some reason I feel the

                  MyList.Count
                  should be
                  MyList.Count - 1

                  I've played with array lists in the past and have found this to always be the case; I just cant remember why :-)

                  I would keep it like this for now see what the exception is (probably index out of bounds), then change it to what I *think* the root of the problem is (Count - 1)

                  Please keep me updated; I'm curious

                  Thanks
                  mrlogik
                  NinjaTrader Ecosystem Vendor - Purelogik Trading

                  Comment


                    #10
                    Originally posted by mrlogik View Post
                    For some reason I feel the
                    MyList.Count
                    should be
                    MyList.Count - 1
                    Well this is relatively fresh in my mind. The last index in a collection is Count-1. So there are basically two ways to test for this:

                    1) if( index <= Count-1 )

                    or

                    2) if( index < Count ) //Slight more efficient I'm guessing.

                    Comment


                      #11
                      I agree, but for some code segment I use, I have to use (index < Count - 1) otherwise I get an exception. Are you doing anything like [index + 1] referencing in you loop?

                      Depending on what you're doing, you could use a foreach statement, this way you can avoid using an index entirely.
                      mrlogik
                      NinjaTrader Ecosystem Vendor - Purelogik Trading

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by frankthearm, Today, 09:08 AM
                      10 responses
                      35 views
                      0 likes
                      Last Post frankthearm  
                      Started by GwFutures1988, Today, 02:48 PM
                      0 responses
                      2 views
                      0 likes
                      Last Post GwFutures1988  
                      Started by mmenigma, Today, 02:22 PM
                      1 response
                      3 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Started by NRITV, Today, 01:15 PM
                      2 responses
                      9 views
                      0 likes
                      Last Post NRITV
                      by NRITV
                       
                      Started by maybeimnotrader, Yesterday, 05:46 PM
                      5 responses
                      28 views
                      0 likes
                      Last Post NinjaTrader_ChelseaB  
                      Working...
                      X