Script Blocks

Idea created by mrwatson-gbs on Dec 7, 2015
    Active
    Score9
    • anchorbuoy
    • hbrendel
    • mark_scott
    • don-brimar
    • intex
    • disabled_davidthorp
    • mrwatson-gbs
    • robertnaud
    • DavidThorp

    Idea

    Elsewhere a Get( LastErrorLineNumber ) function has been proposed, to help track where an error occurred, for example in a try block. While the information returned by this function may be useful during interactive debugging, hard coding of line numbers in error-handling code would however be a very dangerous habit to get in to - code lines move all the time!

     

    A much better solution would be to have a new script step to name blocks of code and a Get( LastErrorBlock ) function, to return the name of the

     

    Here is an example how such blocks could be used with the new try...catch functionality

     

    Try

     

      Block[ "checking settings"]

        Throw Exception If[ IsEmpty( Settings::RequiredField ) ; -1 ; "Please enter the required settings."  ]

      End Block

     

      Block[ "opening file"]

        Throw Exception If[ not _CF_FileExists( $FolderPath & $FileName ) ; -1 ; "File $%1 is missing in $%2." ; $FileName ; $FolderPath ]

     

        Set Variable[ $Text ; _CF_GetFileContents( $Path ))

        Throw Exception If[ Left( $Text ; 2 ) = "$$" ) ; -1 ; "File $%1 could not be read (error $%3) from folder $%2." ; $FileName ; $FolderPath ; $Text ]

      End Block

     

    Catch Exception

      Set Variable[ $errNr ; Get( LastError ) ]

      Set Variable[ $errMsg ; Get( LastErrorMessage ) ]

      Set Variable[ $errBlock ; Get( LastErrorBlock ) ]

      Show Custom Dialogue[ "Error " & $errNr & " while " & $errBlock & ": " & $errMsg ]

     

    End Try

     

    Blocks can also just be used to group code together logically, so that the code can be collapsed Thus the above Try block would look something like this when collapsed:

     

      Try

    >  Block[ "checking settings"]…

    >  Block[ "opening file"]…

    > Catch Exception…

      End Try


    [[[[

    Hm, not sure about this bit:

    Blocks could also be used to scope certain options

     

      Block

     

        # Turn error capture on *for this block*

        Set Error Capture[On]

     

      # This error is caught

        Perform Find[ restore ]

     

      End Block

      # After the end of the block, the error capture state is returned to its previous setting

     

      # This error may or may not be caught - depends on PREVIOUS state of error capture

      Perform Find[ restore ]

     

      Block[ Show Toolbar ]

        Perform Find[ restore ]

      End Block

    ]]]]


    Benefits

    • semantic blocks
    • more readable code
    • greater productivity
    • more maintainable code

     

    Use Cases

     

    • scripting long processes
    • error handling (e.g. with try catch)