Printing line numbers in exceptions like in Java?

D

Digital Puer

Hi, I am interested in catching exceptions and printing the
line number and file name of where the exception occurred,
like what is done in Java. For example, when vector's at()
accesses beyond its size and throws an exception, I would like
to have the file and line information available.

Any ideas how I can do this?
 
M

mlimber

Hi, I am interested in catching exceptions and printing the
line number and file name of where the exception occurred,
like what is done in Java. For example, when vector's at()
accesses beyond its size and throws an exception, I would like
to have the file and line information available.

Any ideas how I can do this?

Use the __FILE__ and __LINE__ macros as part of your exception
message. If you're throwing your own exception with a constructor like
this:

MyException(
const char* msg,
const char* filename,
unsigned line );

you can create a macro to assist... something like:

#define THROW_MY_EXCEPTION( msg ) throw MyException( (msg), __FILE__,
__LINE__ )

If you're wanting to get that info from the standard library (or any
other piece of code over which you don't have control), you'll have to
either use your debugger (many can stop a the point when an exception
is thrown) or catch the exception and throw a new one:

try
{
vector<int> v;
cout << v.at( 0 );
}
catch( const std::exception& e )
{
THROW_MY_EXCEPTION( e.what() );
}

Cheers! --M
 
K

kwikius

Hi, I am interested in catching exceptions and printing the
line number and file name of where the exception occurred,
like what is done in Java. For example, when vector's at()
accesses beyond its size and throws an exception, I would like
to have the file and line information available.

Any ideas how I can do this?

Below is one simple way to do it. Not recommended for critical errors
though, but OK for debugging. You could also look at how the assert
macro works.

regards
Andy Little

#include <stdexcept>
#include <iostream>
#include <sstream>

std::string line_info(std::string const & error, char const * file ,
long line )
{
std::stringstream s;
s << "EXCEPTION: " << error << " in \"" << file << "\",line:" <<
line;
return s.str();
}
int main()
{
try{
throw std::domain_error( line_info( "Something went
wrong",__FILE__,__LINE__));
}
catch(std::exception & e){
std::cout << e.what() <<'\n';
}
}
 
J

John Harrison

Digital said:
Hi, I am interested in catching exceptions and printing the
line number and file name of where the exception occurred,
like what is done in Java. For example, when vector's at()
accesses beyond its size and throws an exception, I would like
to have the file and line information available.

Any ideas how I can do this?

I recommend using your debugger. Find the line of code in vector::at
that throws the exception, use your debugger to set a break point on
that line, then run your program and wait for the break point to be hit.
Then check your call stack to see where vector::at was called from.

Most debuggers cope pretty well with the STL these days.

john
 
I

Ian Collins

John said:
I recommend using your debugger. Find the line of code in vector::at
that throws the exception, use your debugger to set a break point on
that line, then run your program and wait for the break point to be hit.
Then check your call stack to see where vector::at was called from.
Probably better to set the breakpoint on the exception constructor, if
your STL code is optimised finding where the exception is thrown won't
be easy. Probably not easy if it isn't as well!
 
M

mlimber

Probably better to set the breakpoint on the exception constructor, if
your STL code is optimised finding where the exception is thrown won't
be easy. Probably not easy if it isn't as well!

Possibly better still is setting the debugger to break automatically
when *any* exception is thrown. Of course, there are environments
where (good) debuggers are not available, in which case the methods I
suggested previously may prove more helpful.

Cheers! --M
 
D

Digital Puer

I recommend using your debugger. Find the line of code in vector::at
that throws the exception, use your debugger to set a break point on
that line, then run your program and wait for the break point to be hit.
Then check your call stack to see where vector::at was called from.

Most debuggers cope pretty well with the STL these days.

john



This code is going into in-house production, so it will not be running
with a debugger. I am particularly interested in getting the line
number
and filename from exceptions thrown by the STL data structures.
 
M

mlimber

This code is going into in-house production, so it will not be running
with a debugger. I am particularly interested in getting the line
number
and filename from exceptions thrown by the STL data structures.

Then you have several options: modify your standard library (don't do
it!), ask your library vendor to add such functionality, get a new
library that already supports this, or catch, translate, and throw as
I described elsethread.

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,786
Messages
2,569,625
Members
45,322
Latest member
ClaritaMcI

Latest Threads

Top