DataBasic Exception Handler - Example of Usage

Below is a fragment of pseudo code to show how DataBasic exception handler commands and functions can be used.

Label

Code

Comment

 
ECATCH ERR.A

Defines label ERR.A as the exception label.

Exception handler now available at this level.

 

~snip~

Section of code.

 
ECLEARCATCH

Clears the exception label, ERR.A.

Now no exception handler available at this level.

 
ECATCH ERR.B

Defines label ERR.B as the exception label.

Exception handler now available at this level.

 

~snip~

Section of code.

 
ETHROW 99,"HELP"

Throw a user exception with a value of 99 and a message of "HELP".

Will be handled by the defined exception handler in this case ERR.B

FIX.B

~snip~

Section of code.

 
IF ECATCH.ACTIVE() THEN
   ETHROW 77,"missing MD"
END ELSE
   STOP 201,"MD"
END

Test if a catch has been defined.

If so create a user exception with exception "77" and message "missing MD" and transfer control to defined catch label.

Otherwise stop with error message 201 and parameter "MD".

 

~snip~

Section of code.

 

RETURN

End of main code.

ERR.A

 

Start of first exception handler example: Exception transfer automatically clears the exception label, so no exception handler now available at this level.

 

~snip~

Section of trap code.

 
IF EXCEPT.NUM() # 66 THEN ERETHROW

Pass if exception is not the expected one pass it to another handler.

 
ECATCH ERR.A

Finished handling exception ERR.A.

Redefine ERR.A as the exception handler at this level. Control continues with next statement.

 
GOTO RESTART

At end of exception handling code transfer control to label RESTART.

ERR.B

 

Start of second exception handler example: Exception transfer automatically clears the exception label, so no exception handler now available at this level.

 

~snip~

Section of trap code to handle exception.

 
ECATCH ERR.B

Redefine label ERR.B as the exception label.

Exception handler now available at this level.

 

 

 

 
GOTO FIX.B

Return to normal code.

ERR.C

 

Start of third exception handler example: Exception transfer automatically clears the exception label, so no exception handler now available at this level.

  ~snip~

Section of trap code.

 
PRINT EXCEPT.NUM()

Print the last exception number.

 
PRINT EXCEPT.MSG()

Print the last exception message.

 
A = EXCEPT.STK()
LINES = DCOUNT(A,@AM)
FOR I = 1 TO LINES
    CRT A<I>
NEXT I

Print the last exception code level return stack.

 
IF EXCEPT.NUM() THEN
   PRINT "Exception processed"
END

Test if exception saved.

It is so it will print "Exception processed".

 
ETHROW EXCEPT.NUM(), EXCEPT.MSG()

Create a new exception with the saved exception number and message but with this code level return stack.

If an earlier level catch has been defined control will pass to that label in the earlier code level. Otherwise the DataBasic debugger will be immediately entered.

ERR.D

 

Start of fourth exception handler example.

Exception transfer automatically clears the exception label, so no exception label defined at this level.

 

~snip~

Section of trap code.

 
IF ECATCH.ACTIVE()THEN
   ETHROW 77
END ELSE
   STOP 33,"MD"
END

Test if a catch has been defined in an earlier code level.

If so throw an exception with number "77" and no message back to the earlier level.

Otherwise stop.