Exception Handling

InstallShield 2015 ยป InstallScript Language Reference

Exception handling lets you separate error handling from the rest of your script code. InstallScript supports exception handling with the Err object and the keywords try, catch, and endcatch.

If an exception is raised during execution of code that follows the try keyword, script processing passes to the next exception handler (that is, code inside a catch/endcatch block). After an exception handler has executed, processing passes to the line after its endcatch keyword. If no exception is raised by code that follows the try keyword, the code in the exception handler is skipped and processing resumes with the line after the endcatch keyword.

An exception can be raised by a call to the Err object's Raise method, which takes from zero to five arguments. You can retrieve the values of those arguments in the exception handler by checking the value of the corresponding Err object properties.

Note: Raised errors must be negative numbers; otherwise, the installation engine converts the error into the appropriate COM error. Therefore, any error code thrown should be a negative number.

The following code sample demonstrates exception handling:

askfile:

AskText( "Path to file?", "", svPathName );

 

try

    if (!Is( FILE_EXISTS, svPathName )) then

        /* If file does not exist, raise an exception.

           (ERR_NOT_EXIST must have been given a value

           in a #define statement. The error number

           must be a negative number.) */

        Err.Raise( ERR_NOT_EXIST );

    endif;

 

    if GetFileInfo ( svPathName, FILE_SIZE,

        nvFileSize, svResult )<0 then

        /* If file information could not be obtained,

           raise an exception. (ERR_NO_INFO must have been

           given a value in a #define statement. The error

           number must be a negative number.) */

        Err.Raise ( ERR_NO_INFO );

    endif;

 

    SprintfBox ( INFORMATION, "File Size", "Size of %s is %ld.",

        svPathName, nvFileSize );

catch

    /* Exception handler. */

    nTemp = Err.Number;

    /* Handle the exception based on its cause. */

    switch (nTemp)

        case ERR_NOT_EXIST:

            if AskYesNo( svPathName +

                " does not exist. Enter another path?", YES )=YES then

                bTryAgain = TRUE;

            endif;

        case ERR_NO_INFO:

            MessageBox ( "Could not get size of " +

                svPathName, INFORMATION );

            bTryAgain = FALSE;

    endswitch;

endcatch;

 

if bTryAgain then

    bTryAgain = FALSE;

    goto askfile;

endif;

Try/catch/endcatch blocks can be nested as in the following example:

try

    /* Normal processing, part 1. */

    try

        /* Normal processing, part 2. */

    catch

        /* Exception handling for part 2. */

    endcatch;

    /* Normal processing, part 3. */

catch

    /* Exception handling for parts 1 and 3. */

endcatch;