Main Index : Reference :

try - catch()


Description

Safeguards a block of code and catches exceptions thrown.

Usage

try
{
  // Safeguarded code
}
catch([any type] exception)
{
  // Do something
}
catch(ExLastException)
{
  // Catches everything
}

Program Structure

try and catch are used to handle any exceptions thrown in a block of code. If an exception is thrown in the try block, the corresponding catch block is executed. These are the exceptions that are thrown by the system during runtime:

Exception Explanation
ExOutOfMemory Couldn't allocate enough memory.
ExToMuchParameter Too many parameters passed.
ExToLessParameter Too few parameters passed.
ExBadType Wrong type passed to function
or type used in the wrong way.
ExNotAFunction Variable type used as function.
ExNotAMethod That's a member variable not a function.
ExNotAMember There's no such member.
ExMemberNotPublic The member is not accessible
(thrown within global functions).
ExIsProtected The member is private
(thrown within class functions).
ExIsPrivate The member is protected
(thrown within class functions).
ExOutOfRange The array index is out of range.
ExDivisionByZero Division by zero.
ExLastException Catches all exceptions.

One can also throw own exceptions using the throw() command. These are caught in the same way as the built-in exceptions.

try blocks can be put inside other try blocks, scope wise. If no matching catch block is found for an exception, it is passed on to the surrounding try block. If an exception isn't caught before it reaches the outermost level, an Unhandled Exception dialog is displayed.

Example

try
{
  var a = 0;
  var b = 5 / a;
}
catch(ExDivisionByZero)
{
  // Do something
}


// Using the ExLastException keyword one can
// "hide" all exceptions if one wants.

main()
{
  try
  {
    // Buggy code
  }
  catch(ExLastException)
  {
    // It cannot be my fault, can it? :-)
    TextDialog("User error. Please consult the manual that came with your computer.");
  }
}

See Also

throw()