Support for try-catch in CRMScript

Hi all,

We are currently implementing a simple system for try-catch in CRMScript. This will allow you to better handle error-situations in your own scripts and react accordingly. The syntax will most likely be like this:

// Example of catching one of our Exceptions
try {
  printLine("Before execution");

  SearchEngine se;
  se.addField("foo.bar");
  se.execute(); // Will fail

  printLine("After execution");
}
catch {
  printLine("Exception caught: " + error);
}
printLine("After try-catch");

// Example of throwing your own
try {
  throw "This is my own error";
}
catch {
  printLine("Exception caught: " + error);
}

(As you can see, the exception will be available as a string in a implicitly declared variable named "error" in the catch block).

We hope this will be a useful feature, but this post is also a heads-up! The words "try", "catch" and "throw" are will become reserved words in CRMScript as of when this is released (most likely our next release in a couple of weeks). If you have any existing scripts with variables or functions with these names, they will fail. This is similar to when we introduced structs, so my general advice is of course to not use standard programming language reserved words in your scripts :-)

Sverre

RE: Support for try-catch in CRMScript

This is... Awesome! YOU ROCK! :D 

Av: Simen Mostuen Iversen 16. mar 2018

RE: Support for try-catch in CRMScript

Yeah! Great!

Av: Thomas Akervall 16. mar 2018

RE: Support for try-catch in CRMScript

Very cool!

Good job

Av: Eirik Rusten 16. mar 2018

RE: Support for try-catch in CRMScript

VERY useful! This will be used a lot!

Av: Frode Lillerud 16. mar 2018

RE: Support for try-catch in CRMScript

Nice Addition!

Quick Q, error won't be a reserved name?

Av: David Hollegien 16. mar 2018

RE: Support for try-catch in CRMScript

I assume 'error' is a variable, not a keyword. So since it's inside the catch-block you should be fine to use "String error" in your code as well.

* Sverre, how will unhandled exceptions be handled?

F.instance, this script:

#setLanguageLevel 3;

throw "This is an unhandled exception outside a try-catch";

* Would it be possible to have a Trigger called "Unhandled exception in script"?

* Sverre, the 'error' parameter looks like a plain String. Would it be possible get access to a more complex object as well? Perhaps a 'errorObject', 'exception' or similar? That object-definition could look somewhat like this:

struct Exception {
  String Type;
  String ErrorMessage;
  String ScriptIncludeName;
  Integer ScriptCodeLine;
  String StackTrace;
}

Imagine this scenario where it would be difficult to see where the problem comes from when the 'error' is just a string.

try {
  Integer a = getCgiVariable("a").toInteger();
  Integer b = getCgiVariable("b").toInteger();

  Integer c = a / b;
  Integer d = b / a;
  Integer e = c / d;
  
} catch {
  log(error); //Hmm, I wonder where this error occured...?
  //Linenumbers in the Exception object would have been nice ;)
}
Av: Frode Lillerud 16. mar 2018

RE: Support for try-catch in CRMScript

Hi,

I really appreciate your feedback, and your desire to make CRMScript better :-) At the same time, we are currently focusing on some other projects so I cannot implement all the cool features you'd like. I managed to get the try-catch solution in place because I suddenly realized it was a very simple feature to implement, using the standard C++ try-catch functionality at its core. Some of the solutions you are suggesting would require more development, and we simply do not have time to prioritize that right now.

Still, I have managed to include one more detail. In the catch block you can now access two strings: "error" and "errorLocation". The last one contains the location where the exception was thrown. I agree that returning an instance of a class (or a struct) would be more sexy, but in reality it does not make a huge difference.

As pointed out by others, "error" and "errorLocation" are just regular string variables implicitly declared inside the catch-block. Any variables you have with the same names outside that scope will not be influenced, but inside the catch-block, these two variables will take precedence. 

If you throw an exception outside try {}, it will eventually be caught by us and shown as if the script failed, just like other exceptions (such as query-errors).

Sverre

Av: Sverre Hjelm 18. mar 2018

RE: Support for try-catch in CRMScript

Hey, thanks, "errorLocation" is a nice addition, so that's helpful.

I totally get the prioritization cunundrum, so no worries. I'll keep wishing for features, and hopefully some of the wishes get picked up when possible :)

Av: Frode Lillerud 19. mar 2018

RE: Support for try-catch in CRMScript

Awesome Sverre!! :)

Av: Kim Long Nguyen 19. mar 2018

RE: Support for try-catch in CRMScript

Lovely, this is available now in the current Online version (SuperOffice 8.2 Build Release82_C-2018.04.17-03).

Av: Frode Lillerud 20. apr 2018