# Can't run script after engine exception

**URL:** <https://www.ruby-forum.com/t/cant-run-script-after-engine-exception/129508>\
**Category:** IronRuby\
**Created:** [February 19, 2008, 5:17am UTC](https://www.ruby-forum.com/t/cant-run-script-after-engine-exception/129508 "2008-02-19T05:17:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![acampos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@acampos](https://www.ruby-forum.com/u/acampos)\
**Post date:** [February 19, 2008, 5:17am UTC](https://www.ruby-forum.com/t/cant-run-script-after-engine-exception/129508/1 "2008-02-19T05:17:21Z")

</div>

Hi All,

I’ve got a problem where if I execute a Ruby script that has a syntax  
error or missing method and that correctly fails I cannot use that  
engine object to subsequently execute a valid script. If I restart the  
application and re-execute the corrected script it works. It’s only  
executing an erroneous script and then a valid script that fails.

The code I’m using to run the script is:

IScriptEnvironment scriptEnvironment = IronRuby.CreateRuntime();  
IScriptEngine rubyengine = IronRuby.GetEngine(scriptEnvironment);  
SourceUnit scriptSource =  
rubyengine.CreateScriptSourceFromString(myScript);  
IScriptScope scope = rubyengine.CreateScope();  
rubyengine.Execute(scope, scriptSource);

Does anyone know if there is anyway to re-initialise the Ruby engine  
after a syntax or methodmissing exception so scripts can be executed  
again without needing to restart the application?

Regards,

Aaron

---

<div class="post-metadata">

**Author:** ![acampos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@acampos](https://www.ruby-forum.com/u/acampos)\
**Post date:** [February 19, 2008, 5:33am UTC](https://www.ruby-forum.com/t/cant-run-script-after-engine-exception/129508/2 "2008-02-19T05:33:29Z")

</div>

Could you sent a full repro containing the source code of both scripts  
and the order of execution?

Tomas

---

<div class="post-metadata">

**Author:** ![acampos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@acampos](https://www.ruby-forum.com/u/acampos)\
**Post date:** [February 19, 2008, 7:08am UTC](https://www.ruby-forum.com/t/cant-run-script-after-engine-exception/129508/3 "2008-02-19T07:08:47Z")

</div>

Tomas M. wrote:

> Could you sent a full repro containing the source code of both scripts  
> and the order of execution?
> 
> Tomas

I thought I’d made a mistake for a while as I was having real difficulty  
reapeating the problem. However I’ve found that it’s a particular type  
of syntax error that causes the problem rather than any syntax error.  
The end statement missing on an if block is the culprit. Below is the  
code that illustrates the problem:

IScriptEnvironment scriptEnvironment = IronRuby.CreateRuntime();  
IScriptEngine rubyengine = IronRuby.GetEngine(scriptEnvironment);

string goodScript =  
@"  
if true  
puts ‘Ruby is cool’  
end";

string badScript =  
@"  
if true  
puts ‘Ruby is cool’  
";

Console.WriteLine(“Attempting run1 (good script).”);  
rubyengine.Runtime.ExecuteSourceUnit(rubyengine.CreateScriptSourceFromString(goodScript));

try  
{  
Console.WriteLine(“Attempting run2 (bad script).”);  
rubyengine.Runtime.ExecuteSourceUnit(rubyengine.CreateScriptSourceFromString(badScript));  
}  
catch (Exception excp)  
{  
Console.WriteLine(excp.GetType());  
}

try  
{  
Console.WriteLine(“Attempting run3 (good script).”);  
rubyengine.Runtime.ExecuteSourceUnit(rubyengine.CreateScriptSourceFromString(goodScript));  
}  
catch (Exception excp2)  
{  
Console.WriteLine(excp2.GetType());  
}

Output:

Attempting run1 (good script).  
Ruby is cool  
Attempting run2 (bad script).  
Microsoft.Scripting.SyntaxErrorException  
Attempting run3 (good script).  
Microsoft.Scripting.SyntaxErrorException

Regards,

Aaron

---

<div class="post-metadata">

**Author:** ![acampos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@acampos](https://www.ruby-forum.com/u/acampos)\
**Post date:** [February 19, 2008, 8:52am UTC](https://www.ruby-forum.com/t/cant-run-script-after-engine-exception/129508/4 "2008-02-19T08:52:32Z")

</div>

Thanks, will investigate.

Tomas

---

<div class="post-metadata">

**Author:** ![acampos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@acampos](https://www.ruby-forum.com/u/acampos)\
**Post date:** [February 19, 2008, 1:26pm UTC](https://www.ruby-forum.com/t/cant-run-script-after-engine-exception/129508/5 "2008-02-19T13:26:27Z")

</div>

Note that this happens in rbx as well, if you make a syntax error, you  
can’t do any thing any more and have to exit the interpreter.

On Feb 18, 2008 11:32 PM, Tomas M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> IScriptEnvironment scriptEnvironment = IronRuby.CreateRuntime();  
> Regards,  
> removed\_email\_address@domain.invalid  
> [http://rubyforge.org/mailman/listinfo/ironruby-core](http://rubyforge.org/mailman/listinfo/ironruby-core)

–  
Michael L.  
[Polymath Programmer]  
[http://michaeldotnet.blogspot.com](http://michaeldotnet.blogspot.com)
