# Determining line number of runtime errors

#1

Hello all,

I’m just wondering if there’s any way to see what line of the source
code was being executed when an error occurred. This is possible when
the source is being parsed/compiled/interpreted by inspecting
SyntaxErrorException.Lineâ€¦ what about when the source is being
evaluated?

Thanks,
benjamin

#2

You should be able to pass in “-D” to ir.exe to get this information.

On Thu, Dec 11, 2008 at 1:09 AM, Benjamin van der Veen

#3

Has this been answered? I don’t think it has yet. John, Tomas or Curt?

#4

Michael L. wrote:

You should be able to pass in “-D” to ir.exe to get this information.

Oops, perhaps I should have been more specific about what I’m doing. I’m
not actually using ir.exe, I’m creating
ScriptRuntime/ScriptEngine/ScriptScope/ScriptSource in C# code.

I’m wrapping the call to aScriptSource.Execute(aScriptScope) in a
try-catch block. One of my catch blocks explicitly catches a
SyntaxErrorException, which as I mentioned before has a Line property. I
notice that runtime errors cause many different type of exceptions, so
I’m speculating that there must be some other mechanism for determining
what line the error occurred on.

Thanks,
benjamin

#5

From the hosting interface, you need to manually set RuntimeSetup.DebugMode = true when creating the ScriptRuntime.

#6

Curt H. wrote:

From the hosting interface, you need to manually set RuntimeSetup.DebugMode = true when creating the ScriptRuntime.

I saw that option and enabled it (this is what the -D flag does in
ir.exe), but I’m not sure where I get the line number when an error
occurs. I haven’t had a chance to sit down to study the source of ir.exe
to see how it does it, but just by browsing through intellisense and
using the immediate window in VS, I couldn’t find any relevant
properties (or properties of properties) on the thrown exception or any
of the other objects involved (the runtime, the engine, the scope, or
the source). I must be missing something. Any ideas?

Thanks,
benjamin

#7

I think the logic for this is all in RubyExceptionData.cs. You should
be able to use RubyExceptionData.GetInstance to get a RubyExceptionData
object for the thrown exception and then access the Backtrace property
on this object to get the frame information.

#8

Curt H. wrote:

I think the logic for this is all in RubyExceptionData.cs. You should
be able to use RubyExceptionData.GetInstance to get a RubyExceptionData
object for the thrown exception and then access the Backtrace property
on this object to get the frame information.

Hm, the Backtrace property has the stack frame in question, but contains
no useful information. Consider the following program (output is below):

ScriptRuntimeSetup runtimeSetup = new ScriptRuntimeSetup();
runtimeSetup.DebugMode = true;
ScriptRuntime runtime = new ScriptRuntime(runtimeSetup);
ScriptEngine engine = runtime.GetRubyEngine();
ScriptScope scriptScope = engine.CreateScope();
ScriptSource source = engine.CreateScriptSourceFromString(
@"
a = nil
a.blarg();
");

try
{
source.Execute(scriptScope);
}
catch (Exception e)
{
RubyExceptionData red = RubyExceptionData.GetInstance(e);
Console.WriteLine("Oh no! " + e.Message);
foreach(MutableString l in red.Backtrace)
Console.WriteLine(l.ConvertToString());
}

• output -

Oh no! undefined method blarg' for nil:NilClass :0 H:\path\to\my\Program.cs:36:inMain’

I might expect the second line of output (":0") to look something like
“:3”. Have I missed something in my setup of all the IronRuby classes?
I’ve compiled both my project and IronRuby in debug mode.

Thanks,
benjamin

#9

Hello all,

I hate to bump posts, but here I am doing so. I find it surprising that
no one knows how to determine the line numbers of runtime errors. Curt’s
suggestion to investigate RubyExceptionData.GetInstance() seemed
promising, but the information in it’s Backtrace property was not
correct. Anyone know why this might be the case and how I could go about
fixing it?

Thanks,
benjamin

p.s.: I’m not sure if this is a mailing list as well as a message board;
if this message has no context where you’re reading it, see
http://www.ruby-forum.com/topic/173123