Identify various categories and types of errors
Error statements are developed before testing to dictate what should be
displayed when a test passes or fails. You can handle errors by using the
Throw
, Try...catch
, Finally
, and Retry
statements to handle exceptions. An exception is a regulated jump away from the code run.
The throw
keyword is used to throw an exception enum
value. For example, the statement throw Exception::error;
would be used to throw an error exception. We recommend that you use the
Global::error
method instead of the Exception::error
. The Global::error
can write a message to the Infolog when an error exception is thrown and convert a label automatically into text. You can also write the Global::error("Error message.")
as error("Error message.")
.
The try...catch
keywords are used to process a thrown
exception through the catch. The code is run in the try
code block.
If an error is thrown, the code will jump to run the catch code blocks
sequentially to see if the thrown error is specified to handle the
error. It is a common practice to have the first catch statement handle
the Exception::Error
enum value and leave one catch blank
to catch any unspecified error. The Finally
keyword can be
used with the Try...catch
statement. The code in the
finally
code block will run when the code leaves the try
block
either normally or because of an error. See the following code example.
try
{
// Code here.
}
catch (Exception::Numeric)
{
info("Caught a Numeric exception.");
}
catch
{
info("Caught an exception.");
}
finally
{
// Executed no matter how the try block exits.
}
The retry
keyword can only be written in a
catch
block. This causes the code to jump back to the
first line in the try
code block. A retry
should be used when the issue can be fixed through code. The
retry
gives the code an additional chance to succeed and
will display an Infolog when the try
block started to run. You must
make sure that the retry
statement does not cause an infinite
loop. We recommend that you include a variable in the try
block to test if you are in an infinite loop.
You can throw several error exceptions:
- Break - The user pressed Break or Ctrl + C.
- CLRError - The error occurred while the CLR functionality was being used.
- CodeAccessSecurity - An error occurred while the
CodeAccessPermission.demand
method was being used. - DDEerror - An error occurred while the DDE system class was being used.
- Deadlock - A database deadlock occurred because several transactions are waiting for each other.
- DuplicateKeyException - An error occurred in a transaction that is using Optimistic Concurrency Control. The transaction can be retried.
- DuplicateKeyExceptionNotRecovered - An error occurred in a transaction that is using Optimistic Concurrency Control. The code won't be retried, and this exception cannot be caught in a transaction.
- Error - A fatal error occurred. The transaction has been stopped.
- Internal - An internal error occurred in the development system.
- Numeric - An error occurred when the
str2int
,str2int64
, orstr2num
function was used. - UpdateConflict - An error occurred in a transaction during an update.
- UpdateConflictNotRecovered - An error occurred in a transaction during an update. This exception cannot be retried.
- TransientSQLConnectionError - An error occurred during the query run. The transaction will be canceled. This exception will not be caught within a transaction.