digitalmars.D.learn - problem with exceptions
- steven kladitis (17/17) Oct 02 2015 C:\d\examples>pb2
- Dmitri (4/21) Oct 02 2015 that would the stack of the thread leading up to the exception. I
- steven kladitis (80/104) Oct 02 2015 -------------- code is below
- Dmitri (13/119) Oct 02 2015 I guess it's normal:
- Mike Parker (28/45) Oct 02 2015 The info at the bottom is the backtrace. Compiling with -g should
- Jonathan M Davis via Digitalmars-d-learn (10/27) Oct 02 2015 If you don't want to see a stack trace, then catch the exception and jus...
C:\d\examples>pb2
=>main's first line
=>makeOmelet's first line
=>prepareAll's first line
=>prepareEggs's first line
object.Exception pb2.d(64): Cannot take -8 eggs from the fridge
----------------
0x00402252
0x0040512F
0x00405043
0x00403E48
0x7600338A in BaseThreadInitThunk
0x77A497F2 in RtlInitializeExceptionChain
0x77A497C5 in RtlInitializeExceptionChain
----- I always see the info at the bottom. Is this normal. I was
thinkig I should only the the exception message itself.
--- this is windows 7 32 bit.
Oct 02 2015
On Friday, 2 October 2015 at 11:44:21 UTC, steven kladitis wrote:
C:\d\examples>pb2
=>main's first line
=>makeOmelet's first line
=>prepareAll's first line
=>prepareEggs's first line
object.Exception pb2.d(64): Cannot take -8 eggs from the fridge
----------------
0x00402252
0x0040512F
0x00405043
0x00403E48
0x7600338A in BaseThreadInitThunk
0x77A497F2 in RtlInitializeExceptionChain
0x77A497C5 in RtlInitializeExceptionChain
----- I always see the info at the bottom. Is this normal. I
was thinkig I should only the the exception message itself.
--- this is windows 7 32 bit.
that would the stack of the thread leading up to the exception. I
think you get that if you dump the exception object itself (not
just the message).
Oct 02 2015
On Friday, 2 October 2015 at 12:18:36 UTC, Dmitri wrote:On Friday, 2 October 2015 at 11:44:21 UTC, steven kladitis wrote:-------------- code is below import std.stdio; import std.string; void indent(in int level) { foreach (i; 0 .. level * 2) { write(' '); } } void entering(in char[] functionName, in int level) { indent(level); writeln("=>", functionName, "'s first line"); } void exiting(in char[] functionName, in int level) { indent(level); writeln("<=", functionName, "'s last line"); } void main() { entering("main", 0); makeOmelet(-8); eatOmelet(); exiting("main", 0); } void makeOmelet(int eggCount) { entering("makeOmelet", 1); prepareAll(eggCount); cookEggs(); cleanAll(); exiting("makeOmelet", 1); } void eatOmelet() { entering("eatOmelet", 1); exiting("eatOmelet", 1); } void prepareAll(int eggCount) { entering("prepareAll", 2); prepareEggs(eggCount); prepareButter(); preparePan(); exiting("prepareAll", 2); } void cookEggs() { entering("cookEggs", 2); exiting("cookEggs", 2); } void cleanAll() { entering("cleanAll", 2); exiting("cleanAll", 2); } void prepareEggs(int count) { entering("prepareEggs", 3); if (count < 1) { throw new Exception( format("Cannot take %s eggs from the fridge", count)); } exiting("prepareEggs", 3); } void prepareButter() { entering("prepareButter", 3); exiting("prepareButter", 3); } void preparePan() { entering("preparePan", 3); exiting("preparePan", 3); }C:\d\examples>pb2 =>main's first line =>makeOmelet's first line =>prepareAll's first line =>prepareEggs's first line object.Exception pb2.d(64): Cannot take -8 eggs from the fridge ---------------- 0x00402252 0x0040512F 0x00405043 0x00403E48 0x7600338A in BaseThreadInitThunk 0x77A497F2 in RtlInitializeExceptionChain 0x77A497C5 in RtlInitializeExceptionChain ----- I always see the info at the bottom. Is this normal. I was thinkig I should only the the exception message itself. --- this is windows 7 32 bit.that would the stack of the thread leading up to the exception. I think you get that if you dump the exception object itself (not just the message).
Oct 02 2015
On Friday, 2 October 2015 at 12:45:38 UTC, steven kladitis wrote:On Friday, 2 October 2015 at 12:18:36 UTC, Dmitri wrote:I guess it's normal: void main() { throw new Exception("duh"); } --> object.Exception /home/d955/f505.d(3): duh ---------------- ./f505(_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv+0x1f) [0x41e467] ./f505(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2a) [0x41e3c2] ./f505(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll()+0x2b) [0x41e423] ./f505(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2a) [0x41e3c2] ./f505(_d_run_main+0x1d2) [0x41e342] ./f505(main+0x12) [0x41d9ce] /usr/lib/libc.so.6(__libc_start_main+0xf5) [0x40967a15] Looks like the runtime always dumps the uncaught exception that way. If you need it any other way, catch by Exception and print only the message.On Friday, 2 October 2015 at 11:44:21 UTC, steven kladitis wrote:-------------- code is below import std.stdio; import std.string; void indent(in int level) { foreach (i; 0 .. level * 2) { write(' '); } } void entering(in char[] functionName, in int level) { indent(level); writeln("=>", functionName, "'s first line"); } void exiting(in char[] functionName, in int level) { indent(level); writeln("<=", functionName, "'s last line"); } void main() { entering("main", 0); makeOmelet(-8); eatOmelet(); exiting("main", 0); } void makeOmelet(int eggCount) { entering("makeOmelet", 1); prepareAll(eggCount); cookEggs(); cleanAll(); exiting("makeOmelet", 1); } void eatOmelet() { entering("eatOmelet", 1); exiting("eatOmelet", 1); } void prepareAll(int eggCount) { entering("prepareAll", 2); prepareEggs(eggCount); prepareButter(); preparePan(); exiting("prepareAll", 2); } void cookEggs() { entering("cookEggs", 2); exiting("cookEggs", 2); } void cleanAll() { entering("cleanAll", 2); exiting("cleanAll", 2); } void prepareEggs(int count) { entering("prepareEggs", 3); if (count < 1) { throw new Exception( format("Cannot take %s eggs from the fridge", count)); } exiting("prepareEggs", 3); } void prepareButter() { entering("prepareButter", 3); exiting("prepareButter", 3); } void preparePan() { entering("preparePan", 3); exiting("preparePan", 3); }C:\d\examples>pb2 =>main's first line =>makeOmelet's first line =>prepareAll's first line =>prepareEggs's first line object.Exception pb2.d(64): Cannot take -8 eggs from the fridge ---------------- 0x00402252 0x0040512F 0x00405043 0x00403E48 0x7600338A in BaseThreadInitThunk 0x77A497F2 in RtlInitializeExceptionChain 0x77A497C5 in RtlInitializeExceptionChain ----- I always see the info at the bottom. Is this normal. I was thinkig I should only the the exception message itself. --- this is windows 7 32 bit.that would the stack of the thread leading up to the exception. I think you get that if you dump the exception object itself (not just the message).
Oct 02 2015
On Friday, 2 October 2015 at 11:44:21 UTC, steven kladitis wrote:
C:\d\examples>pb2
=>main's first line
=>makeOmelet's first line
=>prepareAll's first line
=>prepareEggs's first line
object.Exception pb2.d(64): Cannot take -8 eggs from the fridge
----------------
0x00402252
0x0040512F
0x00405043
0x00403E48
0x7600338A in BaseThreadInitThunk
0x77A497F2 in RtlInitializeExceptionChain
0x77A497C5 in RtlInitializeExceptionChain
----- I always see the info at the bottom. Is this normal. I
was thinkig I should only the the exception message itself.
--- this is windows 7 32 bit.
The info at the bottom is the backtrace. Compiling with -g should
make it readable (with function names and such).
```
void bar(int i) {
if(i > 10)
throw new Exception("i is too large!!");
}
void main() {
bar(12);
}
```
dmd -g foo
object.Exception foo.d(14): i is too large!!
----------------
0x00402055 in void foo.bar(int) at
C:\LearningD\Chapter05\foo.d(15)
0x00402065 in _Dmain at C:\LearningD\Chapter05\foo.d(19)
0x00402916 in
D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv
0x004028EB in void rt.dmain2._d_run_main(int, char**, extern (C)
int function(char[][])*).runAll()
0x004027FF in _d_run_main
0x004024F4 in main
0x00419F51 in mainCRTStartup
0x75803744 in BaseThreadInitThunk
0x770EA064 in RtlSetCurrentTransaction
0x770EA02F in RtlSetCurrentTransaction
Oct 02 2015
On Friday, October 02, 2015 11:44:19 steven kladitis via Digitalmars-d-learn
wrote:
C:\d\examples>pb2
=>main's first line
=>makeOmelet's first line
=>prepareAll's first line
=>prepareEggs's first line
object.Exception pb2.d(64): Cannot take -8 eggs from the fridge
----------------
0x00402252
0x0040512F
0x00405043
0x00403E48
0x7600338A in BaseThreadInitThunk
0x77A497F2 in RtlInitializeExceptionChain
0x77A497C5 in RtlInitializeExceptionChain
----- I always see the info at the bottom. Is this normal. I was
thinkig I should only the the exception message itself.
--- this is windows 7 32 bit.
If you don't want to see a stack trace, then catch the exception and just
print its msg property. And if you want the file and line number in addition
to the message, then you can access those file the exception's file and line
properties. But the toString function on exceptions is always going to print
out the stack trace in addition to the message, which usually pretty useful,
though in this case, you didn't get the function names as part of the stack
trace, which is less useful. You probably need to build with the debug info
turned on to get it.
- Jonathan M Davis
Oct 02 2015









Dmitri <deemok gmail.com> 