Richard210363
Richard210363

Reputation: 8406

What goes in the Exception field of a Throw Activity

Can anyone tell me how to use the Exception field of the Throw Activity?

I need specific examples. I'm throwing an exception as I need to capture it in the calling code and then run some clean up code.

Thanks for any help

Richard

In reply to Chuck

I tried the following and the workflow cancels but the execution doesn't enter the catches. Any idea why?

public class AbortException : System.Exception
{
}

class manager
{
    ...

    try
    {
        workflowApp.Run();
    }
    catch (AbortException ea)
    {

    }
    catch (Exception ex)
    {

    }

    ...
}

with an Exception Property of: New AbortException

Upvotes: 2

Views: 506

Answers (2)

Maurice
Maurice

Reputation: 27632

You are still using a WorkflowApplication here right?

Is so it executes on a different thread so a C# try/catch around the Run() won't help. You need to add a handler to the OnUnhandledException event as I pointed out in you other question.

WorkflowApplication wfApp = new WorkflowApplication(new YourWorkflow());
wfApp.OnUnhandledException = e => UnhandledExceptionAction.Abort;
wfApp.Run();

Upvotes: 3

Chuck Savage
Chuck Savage

Reputation: 11955

The easiest is to create a class that extends exception and pass any values you want, like:

public class MyError : Exception
{
   public MyError() : base(string.Empty) {}
   public MyError(Exception e) : base(e.Message) {}
   public int MyCustomValue { get; set; }
}

Then using it like, (passing the value of 5 to the catch handler)

throw new MyError(){ MyCustomValue = 5 };

Then in your catch

try{}
catch(MyError ex)
{
    Console.Write(ex.MyCustomValue.ToString());
}

Upvotes: 2

Related Questions