Throwing Exceptions in Jetfire

Note: The complete source example can be found in 'JetfireCoreExampleTests' project, which is part of the release download (V1.0.15 or higher).

This example demonstrates throwing an exception in Jetfire and catching that exception in C#.

Jetfire Code
string jetfireCode =
    @"       
      namespace test
      {
         public workflow Flow
         {
             int i = 0;
             //
             // Set a integer with a value from 1 to 10.
             public int Number
             {
                 set
                 {
                    // throw an exception and catch it in .dot net code.
                    if(value > 10 || value < 1)
                        throw exception(""Bad value"");
                    i = value;
                 }
             }
         }
      }";


C# Unit test.
        [TestMethod]
        public void ThrowExceptionInPropertySetterSet()
        {
            using (
                LinqStartObjects start = JetfireTest.NewNexusServer(
                                JetfireTest.CDRIVE_JETFIRETEST,
                                TjServerStorageType.MemoryLinq,
                                TjStorageCommand.ClearStorage       // deletes any existing storage objects
                                ))
            {
                TjNexus nexus = start.ClientNexus;

                nexus.ParseServer(jetfireCode);
                TjWorkflowClass flowClass = nexus.FindClass<TjWorkflowClass>("test", "Flow");
                TjWorkflow flow = flowClass.New();

                //
                // Establish a baseline for the test
                Assert.AreEqual(0, flow.Get<int>("i"));
                flow.Set("Number", 5);
                Assert.AreEqual(5, flow.Get<int>("i"));
                //
                //  Set an invalid value.
                //
                try
                {
                    flow.Set("Number", 11);
                    Assert.Fail(); // should never be reached.
                }
                catch (JetfireException ex)
                {
                    // Reaching this point of code
                    // indicates a exception occured in the Jetfire code.
                    Assert.AreEqual("Bad value", ex.JetfireMessage);
                    Assert.IsNotNull(ex.CodeException);
                    //
                    //  Try setting the number again.
                    //
                    try
                    {
                        flow.Set("Number", 6);
                        
                    }
                    catch (Exception)
                    {
                        Assert.Fail();
                    }
                }
                Assert.AreEqual(6, flow.Get<int>("i"));

                JetfireTest.AssertNoIncidents(nexus);
            }

        }

Last edited Mar 6, 2009 at 5:26 PM by JohnHansen, version 3

Comments

No comments yet.