Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get error line number in C++ program

I want to handle errors in my c++ program, so I created some exception classes to manage those errors, but I want to specify at which line in my program the error occurred.

I passed LINE macro to the constructor of my exception class.

For example:

void f(int i){ // LINE A
  if(i<0)
    throw(OutOfRange("message", __LINE__); // LINE B
}

void main(){

  try{
    f(-6); // LINE C
  }
  catch(const OutOfRange& error){
    //do something
  }

}

In this example I can only get the LINE B number, but I want to get LINE A and LINE C numbers.

Any idea, where and how to use LINE macro ??

Thanks.

like image 617
CHAKRI Avatar asked Dec 30 '10 11:12

CHAKRI


People also ask

How do I find the line number in an exception?

Simple way, use the Exception. ToString() function, it will return the line after the exception description. You can also check the program debug database as it contains debug info/logs about the whole application.

What is line number in C?

In the C programming language the line number of a source code line is one greater than the number of new-line characters read or introduced up to that point. Programmers could also assign line numbers to statements in older programming languages, such as Fortran, JOSS, and BASIC.

What is the error in the C program?

There are 5 different types of errors in C programming language: Syntax error, Runtime error, Logical error, Semantic error, and Linker error. Syntax errors, linker errors, and semantic errors can be identified by the compiler during compilation.

How do you find the error line in C++?

To find out where the error occurs, you could "open the call stack (Debug > Window > Call Stack) to see what functions are causing the problem. You can double click any line in the call stack to jump to the code and look at variable values from that method in the debugger.


2 Answers

You are looking for a stack trace and there's no portable way to get it. Something somewhat similar can be achieved with:

struct SourcePoint
{
    const char *filename;
    int line;
    SourcePoint(const char *filename, int line)
      : filename(filename), line(line)
    { }
};

std::vector<SourcePoint> callstack;

struct SourcePointMarker
{
    SourcePointMarker(const char *filename, int line)
    {
        callstack.push_back(SourcePoint(filename, line);
    }

    ~SourcePointMarker()
    {
        callstack.pop_back();
    }
}

#define MARK_FUNCTION \
  SourcePointMarker sourcepointmarker(__FILE__, __LINE__);

Then right after the beginning of each function (or point of interest) you just add a line... for example

int myFunction(int x)
{
    MARK_FUNCTION
    ...
}

Using this approach in your error handlers you can know who was called by who and so on (of course you will know only functions or places that have been instrumented with MARK_FUNCTION). If this is needed only during testing (and not in production) then probably you should just enable core dumps and learn how to run a debugger in post-mortem analysis.

like image 104
6502 Avatar answered Oct 15 '22 11:10

6502


You need a stack trace and a debugger. There's no way in Standard C++ that you could find line C without passing it in as an argument (f(-6, __LINE__)), and no way at all that you could find Line A.

like image 1
Puppy Avatar answered Oct 15 '22 13:10

Puppy