hexagon logo

Is there a way to catch errors in a view macro?

I'm trying to achieve something similar to try/catch in c++ or try/except in python.
  • What if you write your macro in Python? That should allow you to use the 'try' construct.
     
    Otherwise there is quite a few functions that perform commands and return an integer (1 if successful and 0 if not). This can be used to catch errors. Here is one example:
    var set var = $_self.workdir string=(eval(GETCWD()))
    var set var=$_self.FMUdir string=(eval($file_name//'_dir'))
    var set var=$_self.createDir string=(eval(MKDIR($_self.FMUdir)))
    if cond=($_self.createDir !=1)
     var set var=$_self.msg str=("Could not create directory " //$_self.FMUdir)
      var set var=$_self.errorFlag int=(EVAL(ALERT2 (msg.self, "ERROR")))
      var del var=$_self.msg
    end
    Here the value of $_self.createDir should be 1. If the directory already exist, a zero will be returned and then a message will be sent to the user.
     
  • Thanks for the advice @Jesper Slattengren​!
     
    I was hoping for something along the lines of placing a chunk of code in an UNDO block and undoing it if it causes an error.
     
    Your first comment is on point. I should probably just write more in python!
  • I am wondering why I get error message when try to run a cmd file that contains $_self. I created a .cmd that contains beneath code:
    var set var = $_self.workdir string=(eval(GETCWD()))
     
    while I get error message that says:
     
    ERROR:  ---------------------------------------------------------------------
    ERROR:  Error detected on line number 4, character 16 of a_test.cmd.
    ERROR:  Invalid character. Enter a legal character.
    ERROR:  The command was not executed.
    ERROR:  >> var set var = $_self.workdir string=(eval(GETCWD()))
    ERROR:  ---------------------------------------------------------------------
     
  • In a cmd, there is no "self". In a macro "$_self" refers to the macro itself and these variables lives only in the macro.
     
    What I often do in the case that you describe is to create a temporary library and create the variables I need there.
    if condition=(!DB_EXISTS(".myTemp"))
     library create library=.myTemp
    end
    var set var=.myTemp.workfir string=(eval(GETCWD())