Getting Stacktrace in VisualStudio

Created at 27 Apr 2021, 23:49
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
AR

arturbrylka

Joined 11.05.2020

Getting Stacktrace in VisualStudio
27 Apr 2021, 23:49


Hi Guys,

I followed the instructions how to attach VS to ctrader with the purpose to debug cbots or indicators using breakpoints. But how do I get the stacktrace of e.g. a Nullpointer exception in VS? Ctrader logs e.g. "29/03/2021 00:01:00.927 | Crashed in OnBar with NullReferenceException: Object reference not set to an instance of an object." Now I need to get the stacktrace or at the least the line of occurrence to start the debugging. But I can not find out how. Thank you in advance!

Kind regards

Artur

 


@arturbrylka
Replies

amusleh
28 Apr 2021, 09:53

Hi,

The message tells you that the exception was thrown inside OnBar method, what you can do is put all your OnBar method code inside a try/catch block and check the thrown exception data or print its stacktrace, do this only for debugging and remove it when you find and fixed the issue because using general try/catch block for Exception type is not a good coding practice.


@amusleh

arturbrylka
28 Apr 2021, 10:22

RE:

amusleh said:

Hi,

The message tells you that the exception was thrown inside OnBar method, what you can do is put all your OnBar method code inside a try/catch block and check the thrown exception data or print its stacktrace, do this only for debugging and remove it when you find and fixed the issue because using general try/catch block for Exception type is not a good coding practice.

Hi Amusleh,

thank you for your comment. Just to avoid misunderstandings let me rephrase and underline the central point of my question:

Is there a possibility to print the stacktrace using the VS debug mode?

Kind regards

Artur


@arturbrylka

amusleh
28 Apr 2021, 11:11

RE: RE:

arturbrylka said:

amusleh said:

Hi,

The message tells you that the exception was thrown inside OnBar method, what you can do is put all your OnBar method code inside a try/catch block and check the thrown exception data or print its stacktrace, do this only for debugging and remove it when you find and fixed the issue because using general try/catch block for Exception type is not a good coding practice.

Hi Amusleh,

thank you for your comment. Just to avoid misunderstandings let me rephrase and underline the central point of my question:

Is there a possibility to print the stacktrace using the VS debug mode?

Kind regards

Artur

Not sure what do you mean by print the stacktrace using the VS debug mode.

If you are debugging and you have a breakpoint somewhere on your code you can step in/out to find the issue but that might get too much time and it depend on when your code throws the exception.

Instead put a try/cache block like this:

using cAlgo.API;
using System;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Bug : Robot
    {
        protected override void OnBar()
        {
            // Remove this cache block once you fixed the issue
            // Don't use general purpose try/catch blocks in production code
            try
            {
                // put all your OnBar code here
            }
            catch (Exception ex)
            {
                // Now you have the exception and you can do anything you want to with it
                Print(ex.StackTrace);
            }
        }
    }
}

 


@amusleh