Printing Stack Trace on Caught Exception

2 views
Skip to first unread message

Michael Forzano

unread,
Jun 14, 2013, 2:15:58 PM6/14/13
to moo-...@googlegroups.com
Hi All,

As far as I know, there is no way to get the traceback of a caught
exception (as is possible in nearly every other language). Am I wrong?
If not, has anyone created a patch to do this?

Mike

Josh Benner

unread,
Jun 14, 2013, 2:40:51 PM6/14/13
to Michael Forzano, moo-...@googlegroups.com
That is incorrect.

You can specify a variable in the except statement that includes traceback information.

My code will feature things like this:

try
  ... code ...
except e (ANY)
  if (player.wizard)
    player:tell_lines($dev_utils:format_caught_error(e));
  endif
  ... other remediation ...
endtry

Here is my $dev_utils:format_caught_error (expects ANSI):

{input} = args;
{error, msg, val, traceback} = input;
su = $string_utils;
name = this:debug_name(traceback[1][4]);
out = {su:message_text({"[bold][red]", toliteral(error), "[normal]: ", msg, " in [bold][yellow]", name, "[white]:[red]", traceback[1][2], "[white], line [green]", traceback[1][6], "[normal] (this == [bold][cyan]", traceback[1][1], "[normal])"}, #2)};
first = 1;
for call in (`traceback[2..$] ! ANY => {}')
  {_this, _verb, _prog, _verb_loc, _player, _line} = call;
  loc = this:debug_name(_verb_loc);
  out = {@out, su:message_text({"... called from [bold][yellow]", loc, "[white]:[red]", _verb, "[normal] (this == [bold][cyan]", _this, "[normal]), line [bold][green]", _line}, #2)};
endfor
out = {@out, "(end of traceback)"};
return out;


$dev_utils:debug_name() just outputs an object literal or a $-literal if one exists on #0 for the object in question. I don't recall if message_text is in Lambda or not... it's basically just a way to to output lists including non-string values to format them better than just their literal representations (ie: show obj name and ref).
-- 
Josh Benner

--
You received this message because you are subscribed to the Google Groups "MOO Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to MOO-talk+u...@googlegroups.com.
To post to this group, send email to MOO-...@googlegroups.com.

Jason SW

unread,
Jun 14, 2013, 2:46:02 PM6/14/13
to moo-...@googlegroups.com
try
"Normal stuff";
except tb (ANY|MOO error code)
"catch stuff";
endtry
Is this what you need? Or did you already know about this?
Reply all
Reply to author
Forward
0 new messages