ThrowException

Exceptions are used to indicate that an error has occurred while the programming is running.

Description

When we want to define when an error occurs at runtime, we use the Exception statement. Exception objects that describe an error are created. Then, with the keyword Throw they are thrown.

 

C# example

static void CopyObject(SampleClass original)
{
    if (SomeValue == SomeOptions.Nr1)
    {
        throw new System.ArgumentException("Parameter cannot be this value", "original");
    }

}

In this example, when the Method CopyObject receives a parameter with an invalid value, the Method System.ArgumentException(); will be thrown.

 

AI-Framework example

new If(SomeValue.IsEqualTo(SomeOptions.Nr1))
{
   // Do something
},
new ElseIf(SomeValue.IsEqualTo(SomeOptions.Nr2))
{
   // Do something
},
new ElseIf(SomeValue.IsEqualTo(SomeOptions.Nr3))
{
   // Do something
},
new Else
{
   new ThrowException("This is not possible ...")
},

In this example, based on the value of SomeValue (line 1), three different statements are executed (line 3, 7 and 11). If SomeValue has an unexpected value, an exception is thrown (line 15). In runtime a popup would appear with this message (This is not possible ...).