Forum: IronRuby Determining line number of runtime errors

Posted by Benjamin van der Veen (bvanderveen)
on 2008-12-11 07:09
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
Posted by Michael Letterle (Guest)
on 2008-12-11 16:16
(Received via mailing list)
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 
<lists@ruby-forum.com
Posted by Benjamin van der Veen (bvanderveen)
on 2008-12-11 20:56
Michael Letterle 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
Posted by Jim Deville (Guest)
on 2008-12-16 05:34
(Received via mailing list)
Has this been answered? I don't think it has yet. John, Tomas or Curt?
Posted by Curt Hagenlocher (Guest)
on 2008-12-16 06:17
(Received via mailing list)
>From the hosting interface, you need to manually set RuntimeSetup.DebugMode = true when creating the ScriptRuntime.
Posted by Benjamin van der Veen (bvanderveen)
on 2008-12-16 09:41
Curt Hagenlocher 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
Posted by Curt Hagenlocher (Guest)
on 2008-12-16 17:45
(Received via mailing list)
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.
Posted by Benjamin van der Veen (bvanderveen)
on 2008-12-16 20:52
Curt Hagenlocher 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;
runtimeSetup.LanguageSetups.Add(Ruby.CreateRubySetup());
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:in `Main'

--

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

Posted by Benjamin van der Veen (bvanderveen)
on 2009-01-02 06:10
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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.