From Xojo Documentation
Jump to: navigation, search

Class (inherits from Object)

An error occurring at runtime.

ErrorNumber Message Stack


Use runtime exceptions to trap errors so they can be handled. For example, reading from or writing to an array element that does not exist will generate an OutOfBoundsException. Exceptions have no methods or events.

Name Description
CryptoException There was a problem using a Crypto function.
EndException Quit was called, causing the application to quit gracefully. See EndException for very important information.
FunctionNotFoundException A function declared using the Declare statement's "Soft" keyword could not be loaded.
HTMLViewerException There was an error parsing the HTML using HTMLViewer.
IllegalCastException You cast an object to a different class and sent it a message its real class can't accept.
IllegalLockingException Mutex Enter and Leave calls are in the wrong order. You cannot leave before you enter.
InvalidParentException You tried to get the parent of a control using the Parent property of the Control class, but its parent is in a different window.
IOException An error occurred while creating or opening a file.
JSONException A method of the JSONItem class failed.
KeyChainException A method of the KeyChain or KeyChainItem classes failed.
KeyNotFoundException An attempt was made to access a Dictionary item with a key that the dictionary does not contain.
MenuHasParentException A MenuItem has been used several times. Currently only applies to Cocoa applications.
NilObjectException An attempt was made to access an object that does not exist.
ObjCException An Objective-C exception around a Declare statement.
OLEException An OLE-related runtime exception occurred. Handle errors in Office Automation code via the OLEException class.
OutOfBoundsException An attempt was made to read from or write to a value, character, or element outside the bounds of the object or data type.
OutOfMemoryException Raised in certain cases when an operation cannot be completed due to insufficient memory.
PlatformNotSupportedException Raised when a feature is not supported on a specific platform.
XojoScriptAlreadyRunningException The user tried to modify an XojoScript that is already executing or tried to modify the context of the script while it is running.
RegExException The RegEx engine issued a runtime exception. Currently this means that you used an invalid search pattern in a Regular Expression. In the future, other types of regular expression exceptions may be added.
RegistryAccessErrorException You tried to use the RegistryItem class without proper access privileges or tried to use it under any Macintosh OS or Linux. It is a Windows-only feature.
ShellNotRunningException You tried to access an asynchronous or interactive shell session, but the shell was not running.
SOAPException SOAPExceptions can be raised when using a WSDL to define your SOAP function. If the method name does not exist or the parameters passed do not match the WSDL specifications, a SOAPException runtime error will be raised.
SpotlightException An error related to a SpotlightQuery was encountered, such as an invalid query.
StackOverflowException When one routine (method/event handler/menu handler) calls another, memory is used to keep track of the place in each routine where it was called along with the values of its local variables. The purpose of this is to return (when the routine being called finishes) to the previous routine with all local variables as they were before. The memory set aside for tracking this is called the Stack (because you are "stacking" one routine on top of another). If your application runs out of stack space, a StackOverflowException will occur. You should be able to test your application thoroughly enough to prevent this error from occurring.
ThreadAccessingUIException You tried to access the user interface from code that is running in a Thread.
ThreadAlreadyRunningException You tried to change the stack size of a Thread while it was running.
ThreadEndException A Thread is quitting. See ThreadEndException for very important information.
TypeMismatchException You tried to assign to an object the wrong data type.
UnsupportedFormatException You used a string expression that does not evaluate to a number or tried to open or save an unsupported picture format.
UnsupportedOperationException You tried to perform an operation which is not supported.
XMLDOMException This exception may occur during the creation of a DOM document.
XMLException There was an error in parsing XML.
XMLReaderException There was an error in parsing XML using XMLReader.

If you fail to trap a runtime error in an exception block, it will trigger the UnhandledException event of the Application class. You can then handle all runtime exceptions generically within that event handler.

When using the Office Automation plug-ins, you can include an exception block that references these classes. For example:

Exception err as OLEException
MsgBox err.message+" Error No.: "+Str(err.ErrorNumber)

Third-party plug-ins may also incorporate their own types of runtime exceptions.

When you are testing your application in the IDE, execution will stop at the offending line and you will see an error message in your Code Editor. When the error occurs in a built application, the user will see a generic error message (as shown in the Notes section below) and quit - unless you include an exception handler.

Runtime Exceptions are used to catch errors in programming. Without an exception handler, the user will see a generic message box that a particular type of exception has occurred and the app will terminate itself.


The application will quit when the user accepts this messagebox.

Including exception handlers in your code allows you to display information that will help you determine the cause of the problem and prevents your application from quitting automatically.

Say, for example, you are getting an OutOfBoundsException when trying to access an element of an array. You could put in an exception handler to display the number of items in the array and the item number you were attempting to change along with any other useful information that would help you to track down the source of the bug. See the section on OutOfBoundsException for an example of this.

There is another example of exception handling in the section on IllegalCastException.


You can use several Exception blocks, each of which handles a specific type of runtime exception. An exception not caught by one of the blocks will be raised up the calling chain.

Dim f As FolderItem

Catch e As NilObjectException
MsgBox("Nil Object")
Catch e As OutOfBoundsException
MsgBox("Out of Bounds")
Catch e As TypeMismatchException
MsgBox("Type Mismatch")
End Try

The following example checks for the type of exception and handles the exception if it is a NilObjectException. If the exception is not a NilObjectException, it lets execution continue through the calling chain by calling Raise

Dim f As FolderItem
Catch e As NilObjectException
MsgBox("Nil Object")
Catch e As RunTimeException
Raise e
End Try

Getting the Exception Type at Runtime

Introspection provides a means of getting the exception type at runtime for logging or other purposes. You can add this functionality in a general way as follows.

First, define a module, named for example RuntimeExceptionExtension. To it, add the following function.

Function Type(extends e As RuntimeException) As String
Dim t As Introspection.TypeInfo = Introspection.GetType(e)
If t <> Nil Then
Return t.FullName
//this should never happen...
Return ""
End If
End Function

This allows you to write code like the following.

//your code here
Catch e As RunTimeException
MsgBox("An exception of type " + e.Type " + was caught.")
End Try

See Also

Catch, Raise, Exception, Try statements; Nil datatype.

Personal tools

Starting Out
Dig Deeper
More Help