www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Line number of Exception instantiation

reply bearophile <bearophileHUGS lycos.com> writes:
What do you think of the idea of the Exception to remember (so later it can be
shown if uncaught) the line number and file name where it was instantiated?

void foo() {
    auto e = new Exception(""); // instantiation line number here
    throw e;
}
void main() {
    foo();
}

Exceptions get caught, rethrown, stored, etc. But in many cases in my code they
are instantiated close to where the cause was. So knowing such line number can
be useful (if you are using a debugger this is not so useful).

Bye,
bearophile
Mar 04 2010
next sibling parent reply "Nick Sabalausky" <a a.a> writes:
"bearophile" <bearophileHUGS lycos.com> wrote in message 
news:hmovte$2431$1 digitalmars.com...
 What do you think of the idea of the Exception to remember (so later it 
 can be shown if uncaught) the line number and file name where it was 
 instantiated?

 void foo() {
    auto e = new Exception(""); // instantiation line number here
    throw e;
 }
 void main() {
    foo();
 }

 Exceptions get caught, rethrown, stored, etc. But in many cases in my code 
 they are instantiated close to where the cause was. So knowing such line 
 number can be useful (if you are using a debugger this is not so useful).

 Bye,
 bearophile

Doesn't tango's exception tracing do that?
Mar 04 2010
parent reply Fawzi Mohamed <fmohamed mac.com> writes:
On 2010-03-04 21:29:15 +0100, "Nick Sabalausky" <a a.a> said:

 "bearophile" <bearophileHUGS lycos.com> wrote in message
 news:hmovte$2431$1 digitalmars.com...
 What do you think of the idea of the Exception to remember (so later it
 can be shown if uncaught) the line number and file name where it was
 instantiated?
 
 void foo() {
 auto e = new Exception(""); // instantiation line number here
 throw e;
 }
 void main() {
 foo();
 }
 
 Exceptions get caught, rethrown, stored, etc. But in many cases in my code
 they are instantiated close to where the cause was. So knowing such line
 number can be useful (if you are using a debugger this is not so useful).
 
 Bye,
 bearophile

Doesn't tango's exception tracing do that?

sure it does, just import TraceExceptions... The exception will contain the stacktrace at the throwing point (adresses)... and with printOut you can get a symbolic trace (if possible). I have also tried to have a way to get the trace of all threads http://github.com/fawzi/blip/blob/master/blip/util/TraceAll.d it is not perfect, probably I should trade a little bit more of memory and suspend all threads (instead of one at a time), but it can be useful when there is a deadlock and you want to see who is the culprit. that uses signals, so it is unsafe, also it will deadlock if the gc tries to suspend all threads while tracing, but I find it useful all the same, so that you don't have to attach a debugger... Fawzi
Mar 04 2010
parent reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
On 03/04/2010 02:49 PM, Fawzi Mohamed wrote:
 Bye,
 bearophile

Doesn't tango's exception tracing do that?

sure it does, just import TraceExceptions... The exception will contain the stacktrace at the throwing point (adresses)... and with printOut you can get a symbolic trace (if possible).

Can you actually get line number information with the stack trace? With my setup, it looks like line numbers should be in there, but they never get initialized. p.s. TraceExceptions is awesome; I can't believe I never knew about them until a week or two ago
Mar 04 2010
parent Fawzi Mohamed <fmohamed mac.com> writes:
On 2010-03-05 02:05:19 +0100, Ellery Newcomer 
<ellery-newcomer utulsa.edu> said:

 On 03/04/2010 02:49 PM, Fawzi Mohamed wrote:
 
 Bye,
 bearophile

Doesn't tango's exception tracing do that?

sure it does, just import TraceExceptions... The exception will contain the stacktrace at the throwing point (adresses)... and with printOut you can get a symbolic trace (if possible).

Can you actually get line number information with the stack trace? With my setup, it looks like line numbers should be in there, but they never get initialized.

the answer is "it depends", first you have to compile with -g, then windows works (thanks to h3r3tic impl), linux has recently been added by wm4 (after release if I remember correctly), with mac you are out of luck. please not that a solution using addr2line as BCS did should also work on linux, it was not done because being able to give a partial info even if the program was crashing was considered more important. Still you can call addr2line yourself after the fact to get the line information (if available), I had asked for someone to write a stacktrace parser that would complete the stacktrace, or writing a addr2line backend, but nobody stepped up, and now wm4 solution seem to work well.
 p.s. TraceExceptions is awesome; I can't believe I never knew about 
 them until a week or two ago

happy to hear that :)
Mar 05 2010
prev sibling next sibling parent reply BCS <none anon.com> writes:
Hello bearophile,

 What do you think of the idea of the Exception to remember (so later
 it can be shown if uncaught) the line number and file name where it
 was instantiated?
 

It shouldn't be hard, add default parameters to the constructor: this(string msg, string file = __FILE__, int line = __LINE__) { ... } -- ... <IXOYE><
Mar 04 2010
parent Fawzi Mohamed <fmohamed mac.com> writes:
On 2010-03-04 21:38:09 +0100, BCS <none anon.com> said:

 Hello bearophile,
 
 What do you think of the idea of the Exception to remember (so later
 it can be shown if uncaught) the line number and file name where it
 was instantiated?
 

It shouldn't be hard, add default parameters to the constructor: this(string msg, string file = __FILE__, int line = __LINE__) { ... }

yes I always do it, I find it useful information, and it is available even in a fully optimized build...
Mar 04 2010
prev sibling parent "Robert Jacques" <sandford jhu.edu> writes:
On Thu, 04 Mar 2010 13:58:54 -0500, bearophile <bearophileHUGS lycos.com>  
wrote:

 What do you think of the idea of the Exception to remember (so later it  
 can be shown if uncaught) the line number and file name where it was  
 instantiated?

 void foo() {
     auto e = new Exception(""); // instantiation line number here
     throw e;
 }
 void main() {
     foo();
 }

 Exceptions get caught, rethrown, stored, etc. But in many cases in my  
 code they are instantiated close to where the cause was. So knowing such  
 line number can be useful (if you are using a debugger this is not so  
 useful).

 Bye,
 bearophile

You mean, like enforce(false, "message")?
Mar 04 2010