faq | docs | tutorials | guide | reference | contact | home  

TUTORIAL - ERROR HANDLING Previous | Tutorial Home

The FGL environment contains a number of advanced error handing capabilities to assist you in creating, maintaining, and optimizing your FGL applications. The first line of defense is the FGL console. This displays activity related to your executing program or server activity, including warning and error messages. You can also insert your own statements in your program that print to the console using the println( ) family of functions.

Simple println( ) example:
<[
    FUNCTION myfunc( )
        println( "Inside the function" )
        return( 1 )
    END
    
    println( "Program starts here" )
]>
<html>
<head>
 <title>Simple println( ) example</title>
</head>
<body text=black bgcolor=white>
<[
    println( "Preparing to call the function" )
    myfunc( )
    println( "Done with the function" )
]>
</body>
</html>
<[ println( "Program ends here" ]>


--- program output echoed to the console ---

Program starts here
Preparing to call the function
Inside the function
Done with the function
Program ends here

For Active Pages and server content, FGL provides a default handler that is displayed whenever an error occurs in a web-based FGL application. This is provided as a part of the customizable handler capability of the FifthGen Server and is found in the http500.ap page located in the server home directory (c:\fgl\server\home by default).

The FifthGen server is capable of displaying a custom error handler page for any HTTP error code. When such an error is generated the server first looks for a page named httpXXX.ap, where XXX is the numeric HTTP error code. If no handler page is found, the standard error message is displayed. This provides a convenient mechanism to capture, report, and customize HTTP errors.

FGL includes a powerful internal debugging tool to help with the identification and repair of programming errors. When an error is encountered during the running of a program, the debugger will automatically kick in if it is enabled (see the debugger documentation for additional information). The source code containing the error is loaded and the specific line is highlighted. Debugging tools provide real-time environment inspection and a host of debugging functions (step-in, step-over, step-to-cursor, step-to-end, reset-execution-point, etc.).

Programmers do not have to wait for an error to occur to access the powerful features of the internal debugger. Programs can be forced to enter debug-mode through the use of the debugbreak( ) function. This will automatically load the debugger at the specified break point when the debugbreak( ) function is encountered during program execution. For example:

Use of the debugbreak( ) function example:
<html>
<head>
 <title>Use of the debugbreak( ) function example</title>
</head>
<body text=black bgcolor=white>
<[
    for ( i=1; i<=1000; i++ )
        if ( i == 750 )
            debugbreak( )
        end
    end
]>
</body>
</html>

The above example will automatically execute the debugger when the counter variable “i” reaches a value of 750. At that point the program will be paused and the programmer can interact in real-time with the program and its environment. When finished debugging, the program can be resumed at the next line following the debugbreak( ) or the program can be terminated.

FGL provides a significantly more advanced method of dealing with programming errors – both in their detection and correction. This is implemented through the TRY/CATCH functionality. In general, you can wrap a section of code with a TRY/CATCH statement to check if an error has occurred and to programmatically correct the error in real time. If the code in the TRY area generates an error, then the CATCH portion is called and a system error object is passed that contains detailed information about the error. For example:

TRY/CATCH example:
<[
    FUNCTION CountDownToError( )
        for ( i=10; i>=0; i-- )
            ! "1024 / " + i + " = " + sprintf( "%12.2f", 1024 / i ) + "<br>"
        end
        return( 1 )
    END
]>
<html>
<head>
 <title>TRY/CATCH example</title>
</head>
<body text=black bgcolor=white>
<[
  TRY
    CountDownToError( )
  
  CATCH( err )
    ! "<br>"
    ! upper( err.errordescription ) + " [Error " + err.errornum + "], "
    ! "in line #" + err.errorline + " of " + err.errorfunction + ",<br>"
    ! lower( err.errorfile )
  END
]>
</body>
</html>

The TRY/CATCH error handler can also report the “call stack” when an error is encountered. This helps in identifying the exact process that led to the error. For example:

TRY/CATCH with CALL STACK example:
<[
    FUNCTION CountDownToError( )
        for ( i=10; i>=0; i-- )
            ! "1024 / " + i + " = " + sprintf( "%12.2f", 1024 / i ) + "<br>"
        end
        return( 1 )
    END
]>
<html>
<head>
 <title>TRY/CATCH example</title>
</head>
<body text=black bgcolor=white>
<[
  TRY
    CountDownToError( )
  
  CATCH( err )
    ! "<br>"
    ! upper( err.errordescription ) + " [Error " + err.errornum + "], "
    ! "in line #" + err.errorline + " of " + err.errorfunction + ",<br>"
    ! lower( err.errorfile )
    
    cnt = len( err.errorCallStack  )
    if ( cnt )
        ! "<font size=2>"
        ! "<br><br><u>CALL STACK</u>:<br>"
        for ( i=1; i<=cnt; i++ )
            ! err.errorCallStack[i] + "<br>"
        end
    end
  END
]>
</body>
</html>

FGL also allows you to create your own customized error codes for use with the TRY/CATCH functionality. This is achieved through the use of the EXCEPT( ) function. When the runtime environment encounters an EXCEPT statement within a TRY/CATCH, program execution is immediately transferred to the CATCH processing and the numeric equivalent of the customized error code passed to the EXCEPT function is passed in. For example:

Advanced TRY/CATCH example:
<[
    FUNCTION CountDownToError( )
        for ( i=10; i>=0; i-- )
            if ( i == 0 )
                except( 1000 )
            end
            ! "1024 / " + i + " = " + sprintf( "%12.2f", 1024 / i ) + "<br>"
        end
        return( 1 )
    END
    
    FUNCTION myCustomErrorHandler( errnum )
        switch ( errnum )
          case 1000
            return( "Divide by zero error." )
          default
            return( "Unknown error" )
        end
    END
]>
<html>
<head>
 <title>Advanced TRY/CATCH example</title>
</head>
<body text=black bgcolor=white>
<[
  TRY
    CountDownToError( )
  
  CATCH( err )
    if ( type( err ) == "O" )
    	! "<br>"
        ! upper( err.errordescription ) + " [Error #" + err.errornum + "], "
        ! "in line #" + err.errorline + " of " + err.errorfunction + ",<br>"
        ! lower( err.errorfile )
    else
    	! "<br>"
        ! "CUSTOM ERROR #" + err + "<br>"
        ! myCustomErrorHandler( err )
    end
  END
]>
</body>
</html>

#####