http://www.cnblogs.com/foundation/archive/2009/12/17/1626612.html
本节主要介绍WF的异常产生方式;WF流程结束方式;以及Throw,TryCatch,Rethrow ,TerminateWorkflow 的使用
本文例子下载:
http://files.cnblogs.com/foundation/ExceptionWorkflow.rar
本文例子说明
使用[Throw Activity] 抛出异常 |
使用[Throw]抛出异常,不会使宿主崩溃 如果使用VS调试,不会在[Throw]处断住, 异常可被实例的[OnUnhandledException]方法捕获 |
由[Activity]中的Code运行错误产生异常 |
public sealed class ExceptionActivity : CodeActivity { protected override void Execute(CodeActivityContext context) { int v = 1 - 1; double i = 1 / v; } } |
代码运行错误产生异常,不会使宿主崩溃, 如果使用VS调试,会在异常处断住 异常可被实例的[OnUnhandledException]方法捕获 |
1. WF实例如果在运行过程中没有出现问题,[ActivityInstanceState 实例状态]会由[Executing 执行中] 变为 [Closed 关闭]
2. 如果流程
[ActivityInstanceState 实例状态]会由[Executing 执行中] 变为 [faulted 有问题]
3. 如果流程
[ActivityInstanceState 实例状态]会由[Executing 执行中] 变为 [Canceled 取消]
4.如果流程
流程会运行失败,并触出发实例的Aborted
运行失败的流程无[ActivityInstanceState 实例状态]
5. [ActivityInstanceState 实例状态]的 [Closed 关闭],[faulted 有问题],[Canceled 取消]都表示流程结束,并触出发实例的Completed
6. 流程[Aborted 失败],流程会即时消失,触出发实例的Aborted,不会触出发实例的Completed
使用实例的[OnUnhandledException]可以处理异常.
ExceptionActivity |
public sealed class ExceptionActivity : CodeActivity { protected override void Execute(CodeActivityContext context) { int v = 1 -1; double i = 1 / v; } } |
流程 |
//============================================================== static void workflowCompleted(WorkflowApplicationCompletedEventArgs e) { System.Console.WriteLine("完成,实例编号:{0},状态:{1}", e.InstanceId, e.CompletionState.ToString()); }
static void aborted(WorkflowApplicationAbortedEventArgs e) { System.Console.WriteLine("aborted ,实例编号:{1},Reason:{0}", e.Reason.Message, e.InstanceId); } //==============================================================
#region //处理Code异常,无[UnhandledExceptionAction]方式
static void exceptionActivityWorkflow_None() { WorkflowApplication instance = new WorkflowApplication(new ExceptionActivityWorkflow());
instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(workflowCompleted); instance.Aborted = aborted;
instance.Run(); }
#endregion |
当不用[WorkflowApplication.OnUnhandledException]方法处理异常时,流程正常结束.会调用实例的[completed],但状态为[faulted] |
//============================================================== static void workflowCompleted(WorkflowApplicationCompletedEventArgs e) { System.Console.WriteLine("完成,实例编号:{0},状态:{1}", e.InstanceId, e.CompletionState.ToString()); }
static void aborted(WorkflowApplicationAbortedEventArgs e) { System.Console.WriteLine("aborted ,实例编号:{1},Reason:{0}", e.Reason.Message, e.InstanceId); } //==============================================================
#region //处理Code异常,[UnhandledExceptionAction.Abort]方式 static void exceptionActivityWorkflow_Abort() { WorkflowApplication instance = new WorkflowApplication(new ExceptionActivityWorkflow());
instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(workflowCompleted); instance.OnUnhandledException = unhandledException_Abort; instance.Aborted = aborted;
instance.Run(); }
static UnhandledExceptionActionunhandledException_Abort(WorkflowApplicationUnhandledExceptionEventArgs e) { System.Console.WriteLine("unhandledException_Abort:{0}", e.UnhandledException.Message); return UnhandledExceptionAction.Abort; } #endregion |
当用[UnhandledExceptionAction.Abort]方式处理异常时,流程被结束.不会调用实例的[completed],会调用实例的[Aborted] |
//============================================================== static void workflowCompleted(WorkflowApplicationCompletedEventArgs e) { System.Console.WriteLine("完成,实例编号:{0},状态:{1}", e.InstanceId, e.CompletionState.ToString()); }
static void aborted(WorkflowApplicationAbortedEventArgs e) { System.Console.WriteLine("aborted ,实例编号:{1},Reason:{0}", e.Reason.Message, e.InstanceId); } //==============================================================
#region //处理Code异常,[UnhandledExceptionAction.Terminate]方式 static void exceptionActivityWorkflow_Terminate() { WorkflowApplication instance = new WorkflowApplication(new ExceptionActivityWorkflow());
instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(workflowCompleted); instance.OnUnhandledException = unhandledException_Terminate; instance.Aborted = aborted;
instance.Run(); }
static UnhandledExceptionActionunhandledException_Terminate(WorkflowApplicationUnhandledExceptionEventArgs e) { System.Console.WriteLine("unhandledException_Abort:{0}", e.UnhandledException.Message); return UnhandledExceptionAction.Terminate; } #endregion |
当用[UnhandledExceptionAction.Abort]方式处理异常时,流程正常结束.会调用实例的[completed],但状态为[faulted] |
1.如果异常出现在[CancellationScope 取消容器]中,当[UnhandledExceptionAction]方法的返回值为[Cancel]时,会调用[CancellationScope]的[CancelHandler]部分 2.完成[CancelHandler]部分后,流程结束.会调用实例的[OnCompleted ] |
具体例子见 [CancellationScope 取消容器] 主题 |
类名 |
System.Activities.Statements.Throw |
文件 |
System.Activities.dll |
结构说明 |
继承 NativeActivity 是一个 sealed类 override 了 [CacheMetadata方法] 与 [Execute方法] [Exception] 属性 的类型为 InArgument<Exception> |
功能说明 |
要为[Exception]指定一个基于[System.Exception ]的派生类 |
自定义异常类 myException |
public class myException : System.Exception { public myException(string ms) : base(ms) { } } |
流程 |
//============================================================== static void workflowCompleted(WorkflowApplicationCompletedEventArgs e) { System.Console.WriteLine("完成,实例编号:{0},状态:{1}", e.InstanceId, e.CompletionState.ToString()); }
static void aborted(WorkflowApplicationAbortedEventArgs e) { System.Console.WriteLine("aborted ,实例编号:{1},Reason:{0}", e.Reason.Message, e.InstanceId); } //==============================================================
#region //处理[Throw Activity]异常,无[UnhandledExceptionAction]方式 static void throwWorkflow_None() { WorkflowApplication instance = new WorkflowApplication(new ThrowWorkflow());
instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(workflowCompleted);
instance.Aborted = aborted;
instance.Run(); } #endregion |
当不用[WorkflowApplication.OnUnhandledException]方法处理异常时,流程正常结束.会调用实例的[completed],但状态为[faulted] |
//============================================================== static void workflowCompleted(WorkflowApplicationCompletedEventArgs e) { System.Console.WriteLine("完成,实例编号:{0},状态:{1}", e.InstanceId, e.CompletionState.ToString()); }
static void aborted(WorkflowApplicationAbortedEventArgs e) { System.Console.WriteLine("aborted ,实例编号:{1},Reason:{0}", e.Reason.Message, e.InstanceId); } //============================================================== #region //处理[Throw Activity]异常,[UnhandledExceptionAction.Abort]方式 static void throwWorkflow_Abort() { WorkflowApplication instance = new WorkflowApplication(new ThrowWorkflow());
instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(workflowCompleted); instance.OnUnhandledException = unhandledException_Abort; instance.Aborted = aborted;
instance.Run(); } #endregion |
当用[UnhandledExceptionAction.Abort]方式处理异常时,流程被结束.不会调用实例的[completed],会调用实例的[Aborted] |
//============================================================== static void workflowCompleted(WorkflowApplicationCompletedEventArgs e) { System.Console.WriteLine("完成,实例编号:{0},状态:{1}", e.InstanceId, e.CompletionState.ToString()); }
static void aborted(WorkflowApplicationAbortedEventArgs e) { System.Console.WriteLine("aborted ,实例编号:{1},Reason:{0}", e.Reason.Message, e.InstanceId); } //============================================================== #region //处ä|理¤¨ª[Throw Activity]异°¨¬常¡ê,[UnhandledExceptionAction.Terminate]方¤?式º? static void throwWorkflow_Terminate() { WorkflowApplication instance = new WorkflowApplication(new ThrowWorkflow());
instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(workflowCompleted); instance.OnUnhandledException = unhandledException_Terminate; instance.Aborted = aborted;
instance.Run(); } #endregion |
当用[UnhandledExceptionAction.Abort]方式处理异常时,流程正常结束.会调用实例的[completed],但状态为[faulted] |
1.如果异常出现在[CancellationScope 取消容器]中,当[UnhandledExceptionAction]方法的返回值为[Cancel]时,会调用[CancellationScope]的[CancelHandler]部分 2.完成[CancelHandler]部分后,流程结束.会调用实例的[OnCompleted ] |
具体例子见 [CancellationScope 取消容器] 主题 |
TryCatch
类名 |
System.Activities.Statements.TryCatch |
文件 |
System.Activities.dll |
结构说明 |
继承 NativeActivity 是一个 sealed类 override 了 [CacheMetadata方法] 与 [Execute方法] 与[Cancel 方法] override 了 [CanInduceIdle]属性 [Catches]属性 的类型为[Collection<Catch>] [Finally]属性 的类型为[Activity] [Try]属性 的类型为[Activity] [Variables] 属性 的类型为[Collection<Variable>] |
Catch<T>
类名 |
System.Activities.Statements.Catch<TException> |
文件 |
System.Activities.dll |
结构说明 |
继承 Object 是一个 sealed类 范型约束 Catch<TException> : Catch where TException : global::System.Exception [ExceptionType ]属性 的类型为[Type] [Action]属性 的类型为[ActivityAction<TException>] |
功能说明
1. [TryCatch]对[Throw Activity]与[Code 异常]都有效,如果异常被捕获,就不会被抛出,流程会继续向下执行 2. [TryCatch] 类似于 C# 中的 Try catch 构造, 执行 Try 块中出现异常时将执行与该异常最匹配的 Catch 块 ,找到不匹配的 catch 时会将捕获的异常继续抛出,工作流将被终止 3. [TryCatch] 有一个 [Try],用于放置要执行的[Activity] 4. [TryCatch] 有一个 [Catches],用于放置捕获异常的[Catch<T>],如果 [TryCatch]中没的[Catch<T>],异常将会被直接抛出 5. [TryCatch] 有一个 [Finally] 块在 [Try] ,[Catchs]完成后执行。无论是否有异常,都会执行 [Finally] 6. [TryCatch]中可以定义变量
1. [Catch<T>] 只能在 [TryCatch] 内部的[Catches]中使用 2. [Catch<T>] 表示要在[ TryCatch ]中使用的一个 catch 块。 3. 如果在执行[Try] 块中出现异常时,[TryCatch] 会查找匹配的[ Catch]的异常的类型。 4. 使用[Catch<T>]时,要指定要用其捕获的异常类型,可以使用[System.Exception]捕获所有异常
5. [Catch<T>]有一临时参数[exception]用于得到异常信息 |
1. 创建一code activity 名为[ExceptionActivity],在Execute中产生一段代码异常
2. 创建一个继承[ System.Exception]名为[myException]的自定义异常信息类
code activity ExceptionActivity |
public sealed class ExceptionActivity : CodeActivity { protected override void Execute(CodeActivityContext context) { int v = 1 -1; double i = 1 / v; } } |
自定义异常信息类 myException |
public class myException : System.Exception { public myException(string ms) : base(ms) { } } |
流程 |
|
宿主 |
//============================================================== static void workflowCompleted(WorkflowApplicationCompletedEventArgs e) { System.Console.WriteLine("完成,实例编号:{0},状态:{1}", e.InstanceId, e.CompletionState.ToString()); }
static void aborted(WorkflowApplicationAbortedEventArgs e) { System.Console.WriteLine("aborted ,实例编号:{1},Reason:{0}", e.Reason.Message, e.InstanceId); }
static UnhandledExceptionActionunhandledException_Terminate(WorkflowApplicationUnhandledExceptionEventArgs e) { System.Console.WriteLine("unhandledException_Abort:{0}", e.UnhandledException.Message); return UnhandledExceptionAction.Terminate; }
//==============================================================
static void tryCatchWorkflow() { WorkflowApplication instance = new WorkflowApplication(new TryCatchWorkflow());
instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(workflowCompleted); instance.OnUnhandledException = unhandledException_Terminate; instance.Aborted = aborted;
instance.Run(); } |
结果 |
类名 |
System.Activities.Statements.Rethrow |
文件 |
System.Activities.dll |
结构说明 |
继承 NativeActivity 是一个 sealed类 override 了 [CacheMetadata方法] 与 [Execute方法] |
功能说明 |
[Rethrow]与[Throw]功能类似,用于产生异常 [Rethrow]与[Throw]的不同是: 1.[Throw]可以用在任何位置,[Rethrow]只能用在[Catch<T>]中 2.[Throw]使用时要为[Exception]指定一个基于[System.Exception ]的派生类,[Rethrow]则不用
很多时候,当异常捕获处理后,需要将异常再次抛出以便结束程序,这时可以使用[Rethrow],这样可以避免使用[Throw]时重新创建[Exception] |
1. 创建一code activity 名为[ExceptionActivity],在Execute中产生一段代码异常
code activity ExceptionActivity |
public sealed class ExceptionActivity : CodeActivity { protected override void Execute(CodeActivityContext context) { int v = 1 -1; double i = 1 / v; } } |
工作流 |
|
宿主 |
//============================================================== static void workflowCompleted(WorkflowApplicationCompletedEventArgs e) { System.Console.WriteLine("完成,实例编号:{0},状态:{1}", e.InstanceId, e.CompletionState.ToString()); }
static void aborted(WorkflowApplicationAbortedEventArgs e) { System.Console.WriteLine("aborted ,实例编号:{1},Reason:{0}", e.Reason.Message, e.InstanceId); }
static UnhandledExceptionActionunhandledException_Terminate(WorkflowApplicationUnhandledExceptionEventArgs e) { System.Console.WriteLine("unhandledException_Abort:{0}", e.UnhandledException.Message); return UnhandledExceptionAction.Terminate; }
//==============================================================
static void rethrowWorkflow() { WorkflowApplication instance = new WorkflowApplication(new RethrowWorkflow());
instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(workflowCompleted); instance.OnUnhandledException = unhandledException_Terminate; instance.Aborted = aborted;
instance.Run(); } |
结果 |
类名 |
System.Activities.Statements.TerminateWorkflow |
文件 |
System.Activities.dll |
结构说明 |
继承 NativeActivity 是一个 sealed类 override 了 [CacheMetadata方法] 与 [Execute方法] [Exception]属性 的类型为[InArgument<System.Exception>] [Reason]属性 的类型为[InArgument<string> ] |
功能说明 |
[Reason]属性,异常信息. 要为[Exception]指定一个基于[System.Exception ]的派生类
当用[TerminateWorkflow]结束流程时,流程正常结束.会调用实例的[completed],但状态为[faulted] |
流程 |
|
宿主 |
#region //TerminateWorkflow例子 static void terminateWorkflow() { WorkflowApplication instance = new WorkflowApplication(new TerminateWorkflow());
instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(terminateWorkflowCompleted);
instance.Run(); }
static void terminateWorkflowCompleted(WorkflowApplicationCompletedEventArgs e) { System.Console.WriteLine("完成,实例编号:{0},状态:{1}", e.InstanceId, e.CompletionState.ToString()); System.Console.WriteLine("Message:{0}", e.TerminationException.Message); } #endregion |
结果 |