![]() |
|
|||||||
| General Programming General NinjaScript programming questions. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
Join Date: Jan 2005
Location: , ,
Posts: 218
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
Administrator
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
|
>> 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.
Dierk
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Certified NinjaScript Consultant
Join Date: Sep 2006
Location: New York, USA
Posts: 774
Thanks: 1
Thanked 7 times in 5 posts
|
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)
}
}
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;
"You look closely enough, you can find everything has a ... weak spot where it can break, sooner or later"
PureLogikTrading |
|
|
|
|
|
#4 |
|
Administrator
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
|
Second thought: I believe Exception.ToString() or Exception.StackTrace might hold the info you are looking for.
Dierk
NinjaTrader Customer Service |
|
|
|
|
|
#5 |
|
Senior Member
Join Date: Jan 2005
Location: , ,
Posts: 218
Thanks: 0
Thanked 0 times in 0 posts
|
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.
|
|
|
|
|
|
#6 |
|
Senior Member
Join Date: Jan 2005
Location: , ,
Posts: 218
Thanks: 0
Thanked 0 times in 0 posts
|
Thanks mrlogik for the format code tip:
Code:
TestFormat()
{
//Space Works!
//No tab, best I can tell. :(
}
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.
|
|
|
|
|
|
#7 |
|
Certified NinjaScript Consultant
Join Date: Sep 2006
Location: New York, USA
Posts: 774
Thanks: 1
Thanked 7 times in 5 posts
|
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 |
|
|
|
|
|
#8 |
|
Senior Member
Join Date: Jan 2005
Location: , ,
Posts: 218
Thanks: 0
Thanked 0 times in 0 posts
|
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); } |
|
|
|
|
|
#9 |
|
Certified NinjaScript Consultant
Join Date: Sep 2006
Location: New York, USA
Posts: 774
Thanks: 1
Thanked 7 times in 5 posts
|
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 |
|
|
|
|
|
#10 | |
|
Senior Member
Join Date: Jan 2005
Location: , ,
Posts: 218
Thanks: 0
Thanked 0 times in 0 posts
|
Quote:
1) if( index <= Count-1 ) or 2) if( index < Count ) //Slight more efficient I'm guessing. |
|
|
|
|
|
|
#11 |
|
Certified NinjaScript Consultant
Join Date: Sep 2006
Location: New York, USA
Posts: 774
Thanks: 1
Thanked 7 times in 5 posts
|
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 |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |