NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Development Support > General Programming

General Programming General NinjaScript programming questions.

Reply
 
Thread Tools Display Modes
Old 12-03-2008, 05:34 AM   #1
shawnj
Senior Member
 
Join Date: Jan 2005
Location: , ,
Posts: 218
Thanks: 0
Thanked 0 times in 0 posts
Default 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
shawnj is offline  
Reply With Quote
Old 12-03-2008, 05:42 AM   #2
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Default

>> 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.
NinjaTrader_Dierk is offline  
Reply With Quote
Old 12-03-2008, 05:51 AM   #3
mrlogik
Certified NinjaScript Consultant
 
mrlogik's Avatar
 
Join Date: Sep 2006
Location: New York, USA
Posts: 774
Thanks: 1
Thanked 7 times in 5 posts
Default

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 < whateveryourlongestperiodis)
    return;
to avoid any bar referencing problems.
"You look closely enough, you can find everything has a ... weak spot where it can break, sooner or later"

PureLogikTrading
mrlogik is offline  
Reply With Quote
Old 12-03-2008, 06:16 AM   #4
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Default

Second thought: I believe Exception.ToString() or Exception.StackTrace might hold the info you are looking for.
NinjaTrader_Dierk is offline  
Reply With Quote
Old 12-03-2008, 01:37 PM   #5
shawnj
Senior Member
 
Join Date: Jan 2005
Location: , ,
Posts: 218
Thanks: 0
Thanked 0 times in 0 posts
Default

Quote:
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.
shawnj is offline  
Reply With Quote
Old 12-03-2008, 01:54 PM   #6
shawnj
Senior Member
 
Join Date: Jan 2005
Location: , ,
Posts: 218
Thanks: 0
Thanked 0 times in 0 posts
Default

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 at 01:57 PM.
shawnj is offline  
Reply With Quote
Old 12-03-2008, 02:38 PM   #7
mrlogik
Certified NinjaScript Consultant
 
mrlogik's Avatar
 
Join Date: Sep 2006
Location: New York, USA
Posts: 774
Thanks: 1
Thanked 7 times in 5 posts
Default

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.
"You look closely enough, you can find everything has a ... weak spot where it can break, sooner or later"

PureLogikTrading
mrlogik is offline  
Reply With Quote
Old 12-04-2008, 04:31 AM   #8
shawnj
Senior Member
 
Join Date: Jan 2005
Location: , ,
Posts: 218
Thanks: 0
Thanked 0 times in 0 posts
Default

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);
}
shawnj is offline  
Reply With Quote
Old 12-04-2008, 05:32 AM   #9
mrlogik
Certified NinjaScript Consultant
 
mrlogik's Avatar
 
Join Date: Sep 2006
Location: New York, USA
Posts: 774
Thanks: 1
Thanked 7 times in 5 posts
Default

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
"You look closely enough, you can find everything has a ... weak spot where it can break, sooner or later"

PureLogikTrading
mrlogik is offline  
Reply With Quote
Old 12-04-2008, 11:15 AM   #10
shawnj
Senior Member
 
Join Date: Jan 2005
Location: , ,
Posts: 218
Thanks: 0
Thanked 0 times in 0 posts
Default

Quote:
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.
shawnj is offline  
Reply With Quote
Old 12-04-2008, 11:34 AM   #11
mrlogik
Certified NinjaScript Consultant
 
mrlogik's Avatar
 
Join Date: Sep 2006
Location: New York, USA
Posts: 774
Thanks: 1
Thanked 7 times in 5 posts
Default

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.
"You look closely enough, you can find everything has a ... weak spot where it can break, sooner or later"

PureLogikTrading
mrlogik is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange exception TheChingachgook Strategy Development 3 10-31-2008 09:45 AM
How to identify open chart-templates ? max-td Charting 6 06-24-2008 10:33 PM
How to identify run-time errors. shawnj General Programming 1 06-08-2008 03:08 PM
how to draw a parallel line to a trend line? tradewiz Charting 4 12-19-2007 04:42 AM
Unhandled Exception HudsonTrader Miscellaneous Support 5 02-04-2005 09:50 AM


All times are GMT -6. The time now is 02:21 PM.