try/catch - getting line number
-
I have a try/catch block that is throwing an exception. What are the different things I can find out about the exception? line number, exception type, etc? thanks!
-
I have a try/catch block that is throwing an exception. What are the different things I can find out about the exception? line number, exception type, etc? thanks!
-
I have a try/catch block that is throwing an exception. What are the different things I can find out about the exception? line number, exception type, etc? thanks!
What I do is to buld a string containing the value of __LINE__ and pass this to a global ThrowError function which packages this into an object which throw can throw. For example: class CMyError : public CException { public: const char * Message; }; and somewhere in the code: static CString s; s.Format("File %s, line %d\nOverflow in addition", THIS_FILE, __LINE__); ThrowError(s); where ThrowError puts its argument into Message in an instance of CMyError and throws the CMyError. So basically I can send any information I want to via a throw. Shraddhan
-
What I do is to buld a string containing the value of __LINE__ and pass this to a global ThrowError function which packages this into an object which throw can throw. For example: class CMyError : public CException { public: const char * Message; }; and somewhere in the code: static CString s; s.Format("File %s, line %d\nOverflow in addition", THIS_FILE, __LINE__); ThrowError(s); where ThrowError puts its argument into Message in an instance of CMyError and throws the CMyError. So basically I can send any information I want to via a throw. Shraddhan
1. Thanks for the suggestion, but I think that is not really what I want. If I know where to throw the error I know where the error is really happening. What I was looking for is line number for unexpected errors. 2. To __yb: I learnt that you can do all with C++ try/catch that you do with SEH through _set_se_translator. So if you can use SEH to get the instruction pointer that caused the error, you can obtain the same using C++ try/catch too. 3. For a suggestion to my own query, I can think of a _dirty_ way to obtain the line number in a try/catch or even with SEH. Before compiling the code write a script that will insert this statement after every line of code:
g_lineno = __LINE__;
and then in your exception filter, you can print this global variable g_lineno. Now what I want to know from you guys is, whether this is going to be a performance hit if I implement it in production. I think _no_big_deal_ but what is your opinion? thanks! -
1. Thanks for the suggestion, but I think that is not really what I want. If I know where to throw the error I know where the error is really happening. What I was looking for is line number for unexpected errors. 2. To __yb: I learnt that you can do all with C++ try/catch that you do with SEH through _set_se_translator. So if you can use SEH to get the instruction pointer that caused the error, you can obtain the same using C++ try/catch too. 3. For a suggestion to my own query, I can think of a _dirty_ way to obtain the line number in a try/catch or even with SEH. Before compiling the code write a script that will insert this statement after every line of code:
g_lineno = __LINE__;
and then in your exception filter, you can print this global variable g_lineno. Now what I want to know from you guys is, whether this is going to be a performance hit if I implement it in production. I think _no_big_deal_ but what is your opinion? thanks!Brundiez wrote:
Now what I want to know from you guys is, whether this is going to be a performance hit if I implement it in production. I think _no_big_deal_ but what is your opinion?
My understanding is that there are quite large overheads in doing a throw / catch. Also, what happens afterwards? Does the code keep running, or do you inform the user? Either way, I reckon it is no big deal. Especially when you include the time taken to actually deal with the line number information. The cost of storing the line number in a global variable is nothing in comparison. But I've just noticed that you say:
Brundiez wrote:
Before compiling the code write a script that will insert this statement after every line of code
*Every* line of code? Do you *really* need to know so precisely? Shraddhan