473,396 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Printing line numbers in exceptions like in Java?

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?

Mar 1 '07 #1
7 6612
On Mar 1, 1:18 pm, "Digital Puer" <digital_p...@hotmail.comwrote:
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<intv;
cout << v.at( 0 );
}
catch( const std::exception& e )
{
THROW_MY_EXCEPTION( e.what() );
}

Cheers! --M

Mar 1 '07 #2
On 1 Mar, 18:18, "Digital Puer" <digital_p...@hotmail.comwrote:
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';
}
}

Mar 1 '07 #3
Digital Puer wrote:
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
Mar 1 '07 #4
John Harrison wrote:
Digital Puer wrote:
>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.
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!

--
Ian Collins.
Mar 1 '07 #5
On Mar 1, 4:10 pm, Ian Collins <ian-n...@hotmail.comwrote:
John Harrison wrote:
Digital Puer wrote:
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.

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

Mar 1 '07 #6
On Mar 1, 12:58 pm, John Harrison <john_androni...@hotmail.comwrote:
Digital Puer wrote:
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


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.

Mar 1 '07 #7
On Mar 1, 6:29 pm, "Digital Puer" <digital_p...@hotmail.comwrote:
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

Mar 5 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Rami | last post by:
Hello All, We have a java application including applets, servlets and ejbs. Users can get reports and print them directly from applets. Our print methods uses PrinterJob and Graphics2d, imports...
21
by: dkcpub | last post by:
I'm very new to Python, but I couldn't find anything in the docs or faq about this. And I fished around in the IDLE menus but didn't see anything. Is there a tool that can determine all the...
2
by: gerb | last post by:
Hello, I realise this is not a pure Javascript question, and that VBscript is probably involved at some point, though not as much as I fear. If you opened this item looking for a pute Javascript...
14
by: Steven T. Hatton | last post by:
I'm trying to write a program like hexel. I guess I could fish out the source for hexel and look at that, but for now I'm trying to figure out how I can do with with std::stringstream and...
8
by: Arun Bhalla | last post by:
Hi, I'm developing an Explorer bar using VS.NET 2003 (C#) on Windows XP. For some time, I've noticed that I don't have filenames and line numbers appearing in my exceptions' stack traces. On...
3
by: Jim Bancroft | last post by:
Hi all, In VB6 I used a 3rd party tool for line numbering my source code, to help with debugging. However, in experimenting with VB .Net I've noticed that my exceptions automatically provide...
2
by: Ben | last post by:
Hello all After years of playing developing stuff for fun and personal use I'm finally developing an app that I am intending to sell. As such, I'm trying to make it as reliable and stable as...
3
by: Rahul | last post by:
Hi, Is there a way to get the line numbers to display in the exception when you have published your web site? Thanks Rahul
3
trkrbabe
by: trkrbabe | last post by:
Hi, this is my first time posting here. It appears that I am taking the same class as a few other people here. I have only been learning Java for about five weeks now. I have my Product class...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.