RuntimeException

From Xojo Documentation

Class (inherits from Object)

An error occurring at runtime.

Properties
ErrorNumber Message Reason fa-lock-32.png
Methods
Stack StackFrames
Constructors

Constructor(message As String="", errorCode As Integer=0)


Notes

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.

Name Description
COMException Raised anytime an exception occurs within any ActiveX/COM component that has been added to your project.
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.
Xojo.Core.InvalidArgumentException Raised when an invalid argument is passed to a function.
InvalidArgumentException Raised when an invalid argument is passed to a function.
InvalidJSONException The value you passed to ParseJSON was not valid JSON.
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.
NamespaceException Raised in a web application when a control is created with the WebSDK but is missing its JavaScript namespace.
NetworkException A network exception occurred with URLConnection.
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.
SessionNotAvailableException Raised if no session is available to attach to a control.
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
MessageBox(err.message + " Error No.: " + err.ErrorNumber.ToString)

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 text is usually "An exception of class [ExceptionFromListAbove] was not handled. The application must shut down." The app quits when the user clicks the OK.

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.

Sample Code

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.

Var f As FolderItem
Try
f.Remove
Catch e As NilObjectException
MessageBox("Nil Object")
Catch e As OutOfBoundsException
MessageBox("Out of Bounds")
Catch e As TypeMismatchException
MessageBox("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

Var f As FolderItem
Try
f.Remove
Catch e As NilObjectException
MessageBox("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
Var t As Introspection.TypeInfo = Introspection.GetType(e)
If t <> Nil Then
Return t.FullName
Else
// this should never happen...
Return ""
End If
End Function

This allows you to write code like the following.

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

See Also

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