Exception handler examples

This section provides several examples of implementing a structured exception handler in your application.

Example 1

This procedure contains sanScript code to handle the exception that can occur when you reference an invalid form by name. If an invalid name is supplied to the open form with name statement, a system exception is thrown. The exception is handled by displaying a message that explains the situation. Notice that the predefined _script_ constant is used to include the name of the script in the message.

in string form_name;

try
	{Open the named form.}
	open form with name form_name;
catch [EXCEPTION_CLASS_SCRIPT_MISSING]
	{Catch the exception if the form is missing.}
	error "Cannot find the form " + form_name + ". Error in script " + _script_ + ".";
end try;

Example 2

This example calls a procedure that uses anonymous parameters. It uses an exception handler to log any parameter type incompatibilities that occur when incorrect parameters are passed to the procedure.

local long exception_subclass;
local string exception_message;

try
	call Calculate_Payment, principle, interest, term, method;
catch [EXCEPTION_CLASS_SCRIPT_BAD_TYPE] 
	{Get the exception subclass.}
	exception_subclass = Exception_SubClass();
	exception_message = Exception_Message();
	call Log_Exception, "Bad Type", exception_subclass, exception_message;
	error "Invalid parameters passed to Calculate_Payment.”;
end try;

Example 3

This example shows how to handle exceptions for common database operations. The following script attempts to read and actively lock a record from the Seller_Data table. Note that the throw system exception for table statement is used to throw a system exception if a database error occurred. If the record can’t be actively locked, the script will retry 10 times before displaying a message to the user. The restart try statement causes the statements in the try...end try block to be executed again, but the values of local variables are not affected.

local integer read_count = 0;

try
	'Seller ID' of table Seller_Data = 'Seller ID' of window Sellers;
	{Attempt to actively lock the record.}
	change table Seller_Data, lock;
	increment read_count;
	{Check the error. If an error occurred, an exception will be
	thrown.}
	throw system exception for table Seller_Data;

catch [EXCEPTION_CLASS_DB]
	{A standard database error occurred.}
	if err() = LOCKED then
		{Retry the read operation 10 times.}
		if read_count < 10 then
			restart try;
		else
			error "Unable to read and actively lock the record.";
		end if;
	end if;
else
	{Some other exception occurred.}
	error "An unknown exception occurred. Class: " +  str(Exception_Class()) + " Subclass: " + str(Exception_SubClass());
end try;

Example 4

This example is a data verification procedure that uses throw statements to throw user exceptions when invalid data values are encountered. Constants have been added to the dictionary for each class and subclass of exception that can be thrown.

[spacer]

Procedure Name

Verify_Buyer_Record


in table Buyer_Data;

if 'Buyer Name' of table Buyer_Data = "" then
	throw BUYER_DATA, NO_NAME, "Buyer ID " + 'Buyer ID' of table Buyer_Data + " has an invalid name.";
end if;

if 'Agent' of table Buyer_Data = "" then
	throw BUYER_DATA, NO_AGENT, "Buyer ID " + 'Buyer ID' of table Buyer_Data + " has an invalid agent.";
end if;

if ('Maximum Price' of table Buyer_Data < 0) or ('Maximum Price' of table Buyer_Data > 1000000) then
	throw BUYER_DATA, INVALID_PRICE, "Buyer ID " + 'Buyer ID' of table Buyer_Data + " has an invalid maximum price.";
end if;

The following script calls the Verify_Buyer_Record procedure. As records are verified, any user exceptions that are thrown are handled by the exception handler. Note that when a user exception is handled, the same record is read again until it produces no user exceptions.

local long exception_subclass;

{Read the first record in the table, so there is a current record.}
get first table Buyer_Data;

try
	{Read the current record.}
	change table Buyer_Data;
	while err() = OKAY do
		call Verify_Buyer_Record, table Buyer_Data;
		change next table Buyer_Data;
	end while;
catch [BUYER_DATA]
	{Get the exception subclass}
	exception_subclass = Exception_SubClass();
	case exception_subclass 
		in [NO_NAME]
			'Buyer Name' of table Buyer_Data = "Unknown";
		in [NO_AGENT]
			'Agent' of table Buyer_Data = "Staff";
		in [INVALID_PRICE]
			'Asking Price' of table Buyer_Data = 0;
	end case;
	{Save the repaired record.}
	save table Buyer_Data;
	{Check the record again}
	restart try;
else
	{Some other exception occurred.}
	error "An unknown exception occurred. Class: " + str(Exception_Class()) + " Subclass: " + str(Exception_SubClass());
end try;

 


Documentation Feedback