This project has moved and is read-only. For the latest updates, please go here.

Error Handling and Error Info?

Jul 27, 2013 at 2:01 PM
Edited Jul 27, 2013 at 2:35 PM
I saw Issue 12 and that you added error handling and information but I can not see how that info is available through any of the c# objects exposed.

In the vb6 script control there is an error property with these properties.

Clear (a method)

If the VB Script Control fails to execute code it doesn't throw an exception or misbehave. I do check the Error.Number afterwards and if in a test mode display the code with a pointer to the line and column to the end user. I assume (we all know what happens to me when I do that) that Clearscript basically calls the same COM code for the windows engine that the VB6 control does it would be easy to implement the error property?

V8 seems to expose pretty much the same error info.

Overall if I had error info I would be set. Your team has gone to stellar level to make it easy to use and carefree. Thank you.

What would be nice is the mother of all usage examples heavily commented. Interested in how to use the new initiation and memory management settings too.

edit : Nosing around I found the ClearScript.ScriptEngineException object. Will experiment to see what that is all about.
Jul 27, 2013 at 6:10 PM
Sounds like you've already found it. ScriptEngineException.ErrorDetails contains engine-specific error properties such as source location, script stack trace, etc. ClearScript currently exposes this information as a single formatted string because the underlying script engines are not consistent in terms of what information they make available in various error scenarios. Please let us know if your application requires access to individual error properties.
Jul 31, 2013 at 11:35 AM
I wrote a script where I forgot to put the end curly bracket to a function. First when I executed the script it threw an error as it should. I put a try catch around the execute statement but when I ran the same script no error was thrown to catch. A bit of an oddity. Does this have something to do with the c++ interface dll?
Jul 31, 2013 at 7:48 PM
Edited Aug 3, 2013 at 3:14 AM
A syntax error should throw an exception every time. We just ran the following, and it produced the expected 10 lines of output:
for (var index = 0; index < 10; index++)
        engine.Evaluate("function foo() {");
    catch (ScriptEngineException exception)
        Console.WriteLine("Exception: " + exception.Message);
Can you provide an example that fails?
Aug 1, 2013 at 2:19 PM
My code was catch(Exception ex) which should of thrown an error too.

I will try it with the more specific ScriptEngineException
Aug 1, 2013 at 3:38 PM
That shouldn't make a difference, and it doesn't in our test. If you can reproduce the issue, please let us know!
Aug 1, 2013 at 11:54 PM
newCode =
"\r\nPublic Sub bc_new_filename()\r\nmr.bcx = 1\r\nmr.bcy = 1\r\nmr.bcw = 1600\r\nmr.bch = 267\r\nmr.bc_dpi = 200\r\nmr.bc_break_bad = true\r\nmr.bc_regex_mask = \"(\d{1,8}\-\d{2})\"\r\n\r\n\r\n\r\n\r\nEnd Sub\r\n\r\nPublic Sub bc_new_output()\r\nif mr.bc_pageno > 1 then\r\n\r\nmr.bc_data = \"SKIP\"\r\n\r\nend if\r\n\r\n\r\nEnd Sub\r\n\r\nPublic Sub log_this_message()\r\nif mr.logfile = \"BC_IMP.log\" then\r\nmr.logit = true\r\nend if"

 //       try
  //      {
            if ((int)whichOne < -10)

  //      }
  //      catch (Microsoft.ClearScript.ScriptEngineException ex)
  //      {
  //          string mymymy = "DOH";
  //      }

with the error trapping commented out I get an error Expected :End

with the error trapping enabled it does not trap any error.

I have tried this with V8, same behavior.

also I can not get any info from Microsoft.ClearScript.ScriptEngineException. There is nothing there. I mean intellisense shows ScriptEngineException but the next . displays no properties although I can see them in object browser.

if I add "End Sub" to newCode all is good.
Aug 2, 2013 at 3:34 AM
Hi notahack!

We couldn't run your sample exactly as shown; the VBScript code string seems to be mangled a bit and the compiler complains about unrecognized escape sequences. However, it really shouldn't matter; simply executing Public Sub bc_new_filename() without the sub terminator should trigger the same behavior.

with the error trapping commented out I get an error Expected :End

What do you mean, exactly? Do you mean that Visual Studio breaks into its debugger and notifies you of the exception?

with the error trapping enabled it does not trap any error.

Your code catches and discards the exception, so unless you've configured Visual Studio to break when an exception is thrown (by default it only breaks when an exception goes uncaught), you'll see no error. Try setting a breakpoint inside the catch clause. You should be able to examine the exception there. Here is what we see:


Good luck!
Aug 2, 2013 at 1:46 PM
Silly silly me or maybe silly microsoft compiler. I had the line string mymymy = "DOH"; in the catch section with a break set to it. The compiler just saw a declaration that does nothing or isn't referenced again and decided to ignore it. If I put the line Console.WriteLine(ex); in the catch section with a break it executes the catch.

Learn something new everyday huh?

Looking at the caught exception object I still don't see where it reports the line and column that erred. The script engine doesn't like blank lines (\r\n) with carriage return and line feeds?
Aug 2, 2013 at 2:17 PM
Looking at the caught exception object I still don't see where it reports the line and column that erred.

If we're still talking about VBScript, it doesn't provide details like that unless debugging is enabled. To enable debugging, pass WindowsScriptEngineFlags.EnableDebugging into the VBScriptEngine constructor.