Class

RuntimeException


Description

An error occurring at runtime.

Properties

Name

Type

Read-Only

Shared

ErrorNumber

Integer

Message

String

Methods

Name

Parameters

Returns

Shared

Constructor

message As String = "", errorCode As Integer = 0

Stack

String

StackFrames

StackFrame

Property descriptions


RuntimeException.ErrorNumber

ErrorNumber As Integer

Used to contain an error number that describes the runtime error.

The following example displays a message box if, for example, you try to create more than one KeychainItem for the same application.

Var newItem As KeychainItem
If System.KeychainCount > 0 Then
  newItem = New KeychainItem
  // Indicate the name of the application
  newItem.ServiceName = "MyApplication"

  // Create a new keychain item for the application and assign the password
  System.Keychain.AddPassword(newItem, "SecretPassword")
Else
  System.Beep
  MessageBox("You don't have a key chain.")
End If
Exception err As KeychainException
  MessageBox(err.Message + ". Error Code: " + err.ErrorNumber.ToString)

RuntimeException.Message

Message As String

Used to contain descriptive text to display when the runtime exception is encountered.

The following example displays a message box if, for example, you try to create more than one KeychainItem for the same application.

Var newItem As KeyChainItem
If System.KeyChainCount > 0 Then
  newItem = New KeyChainItem
  // Indicate the name of the application
  newItem.ServiceName = "MyApplication"

  // Create a new keychain item for the application and assign the password
  System.KeyChain.AddPassword(newItem, "SecretPassword")
Else
  System.Beep
  MessageBox("You don't have a key chain.")
End If
Exception err As KeyChainException
  MessageBox(err.Message + ". Error Code: " + err.ErrorNumber.ToString)

Method descriptions


RuntimeException.Constructor

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

Note

Constructors are special methods called when you create an object with the New keyword and pass in the parameters above.

Used to raise your own RuntimeException with a message and optional error code.


RuntimeException.Stack

Stack As String

Returns a String array that contains a list of all of the methods in the stack from the main entrypoint to the point at which the exception was invoked.

The stack contains all the method names up and including the current method name.

This feature only works if the IncludeFunctionNames property on the App object is selected in the Shared Build Settings.

In addition to your own method calls, you will also see framework method calls, but these may not always be completely accurate due to insufficient symbols for the OS to resolve.


RuntimeException.StackFrames

StackFrames() As StackFrame()

Returns an array containing the stack when the exception was first raised.

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.

DatabaseException

A Database error occurred.

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.

IteratorException

Raised when an error or a change occurs in the data being iterated over.

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.

NotificationException

A notification could not be sent.

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.

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.

XojoScriptException

An invalid MemoryBlock was passed to the Compile method.

If you fail to trap a runtime error in an exception block, it will trigger the UnhandledException event of the DesktopApplication 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

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

Compatibility

All projects types on all supported operating systems.

See also

Object parent class; Catch, Raise, Exception, Try statements; Nil datatype.