Archived

This topic is now archived and is closed to further replies.

Mike737

try catch(...), how to grab the line number

Recommended Posts

Mike737    122
I was just wondering how you could find what line number an error has occurred at when using a try catch(...) statement. I know you can use __LINE__ to grab the current line number, but I want to grab the line number at which the error occurred at. I know this can be done in C# and JAVA, so there must be a way to do it in C++. Anyone have any ideas? Thanks ---- Mike Team AI: Http://members.iinet.net.au/~slyons/teamai

Share this post


Link to post
Share on other sites
Aldacron    4544
It can be done in C# and Java because it's built-in to the runtime. I know for Java it's possible to disable this feature by using the -O option when compiling, which among other things removes stacktrace info from the class files. In C++, you have to implement the solution yourself, but it will only work for custom exceptions. There would be no way to implement it for exceptions thrown by the runtime, AFAIK.

As an example, a custom exception class might have a constructor that accepts the file name and line number as parameters. If you implement a top-level try/catch block to catch all exceptions the app might throw, then you can catch you custom exceptions, extract the line number and sourcefile name to output to a log file or message box. Additionally, through macro magic, you can implement your own stacktrace system for more information.

[edited by - aldacron on January 6, 2004 5:11:22 AM]

Share this post


Link to post
Share on other sites
jenova    122
GetExceptionInformation will give you address but not line number. of course you can get the line number from the address.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
bilsa    157
Maybe you could use the __LINE__ and __FILE__ macros which give you the line and file where you are... so you could send them with the exception as a string?

something like:

throw(BAD_TYPING("Exception occured at line:" ++ __LINE__ ++ " in " ++ __FILE__))

something like that should do it, but this way you will need to have a constructor for BAD_TYPING that takes a string as an argument...

[edited by - bilsa on January 6, 2004 9:21:49 AM]

Share this post


Link to post
Share on other sites
Mike737    122
quote:
Original post by bilsa
throw(BAD_TYPING("Exception occured at line:" ++ __LINE__ ++ " in " ++ __FILE__))

something like that should do it, but this way you will need to have a constructor for BAD_TYPING that takes a string as an argument...



I can't use this as it will not really be that useful unless I physically throw the error. Most of the errors I get are not thrown by my throw statements.


quote:
Original post by jenova
GetExceptionInformation will give you address but not line number. of course you can get the line number from the address.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.



Can you actually implement GetExceptionInformation without the use of a __try{} block?

I am only using a try block and when I try to implement it I recieve "error C2707: '_exception_info' : bad context for intrinsic function" on the line "LPEXCEPTION_POINTERS lpExceptionInfo; lpExceptionInfo = GetExceptionInformation();".

And how would you get the line number from the address?

----
Mike
Portfolio: Http://members.iinet.net.au/~slyons
Team AI: Http://members.iinet.net.au/~slyons/teamai



[edited by - Mike737 on January 6, 2004 9:58:01 PM]

Share this post


Link to post
Share on other sites
Doc    586
quote:
Original post by Mike737
quote:
Original post by bilsa
throw(BAD_TYPING("Exception occured at line:" ++ __LINE__ ++ " in " ++ __FILE__))

something like that should do it, but this way you will need to have a constructor for BAD_TYPING that takes a string as an argument...



I can't use this as it will not really be that useful unless I physically throw the error. Most of the errors I get are not thrown by my throw statements.



Then why in the hell do you need to know the line number?

Oh, and
throw(BAD_TYPING("Exception occured at line:" ++ __LINE__ ++ " in " ++ __FILE__))  

won't work since none of them are string classes, and __LINE__ is really just an int. You'd need to make a BAD_TYPING constructor that can take a char* (for __FILE__) and an int (for __LINE__), and have them converted and concatenated in there.

Image loads when I'm online since I'm too lazy to find a permanent host.The following statement is true. The previous statement is false.
Shameless promotion:
FreePop: The GPL Populous II clone.

[edited by - Doc on January 6, 2004 11:34:01 PM]

Share this post


Link to post
Share on other sites
Mike737    122
quote:
Original post by Doc
quote:
Original post by Mike737
quote:
Original post by bilsa
throw(BAD_TYPING("Exception occured at line:" ++ __LINE__ ++ " in " ++ __FILE__))

something like that should do it, but this way you will need to have a constructor for BAD_TYPING that takes a string as an argument...



I can''t use this as it will not really be that useful unless I physically throw the error. Most of the errors I get are not thrown by my throw statements.



Then why in the hell do you need to know the line number?



This is one of my current bugs at the moment and it has taken me a while to find the line the error is occuring on. If it reported me the line in the first place I would of been able to run a debug through it.


try
{
//lines ommitted

if (CNode->bParentExists)
{
if (CNode->CParentNode->bParentExists)
{
iNewHeading = calculateHeading(CNode->iX,CNode->iY,CNode->CParentNode->iX,CNode->CParentNode->iY);
iOldHeading = calculateHeading(CNode->CParentNode->iX,CNode->CParentNode->iY,CNode->CParentNode->CParentNode->iX,CNode->CParentNode->CParentNode->iY); //Error occurs on this line and is thrown to catch - error is due to bad pointer

iHeadingCost = abs((int)(((iNewHeading - iOldHeading) / 45)*2));
}
}
//lines ommitted

}
catch(...)
{
ErrorException *CErr = new ErrorException("Unhandled exception");
CErr->setSource("AStar","update()");
CErr->displayMessage();
CErr->logError();
delete CErr;
m_bFault = true;
}


BTW. Doc, I checked out your site, what did you use to document the API like that?


----
Mike
Portfolio: Http://members.iinet.net.au/~slyons
Team AI: Http://members.iinet.net.au/~slyons/teamai

Share this post


Link to post
Share on other sites
Doc    586
quote:
Original post by Mike737
This is one of my current bugs at the moment and it has taken me a while to find the line the error is occuring on. If it reported me the line in the first place I would of been able to run a debug through it.


Hmm. Okay, that''s a valid problem. The best I can suggest is to narrow your try/catch blocks. Put a specific try/catch over the line you think the exception is coming from. If it''s not that, try another line.
quote:

BTW. Doc, I checked out your site, what did you use to document the API like that?

Doxygen

Image loads when I''m online since I''m too lazy to find a permanent host.The following statement is true. The previous statement is false.
Shameless promotion:
FreePop: The GPL Populous II clone.

Share this post


Link to post
Share on other sites