12345678910111213141516171819202122232425262728293031 |
- using System;
- using System.Diagnostics;
- namespace TBL.CSharp.Base
- {
- public static partial class Extension
- {
- /// <summary>
- /// 对 <paramref name="action"> </paramref> 的保护性调用
- /// <para>能够避免发生异常时中断程序</para>
- /// </summary>
- /// <param name="action">要执行的过程</param>
- /// <param name="exception">执行过程中抛出的异常,如果未抛出将会为 <see langword="null"/></param>
- /// <returns>是否执行成功</returns>
- public static bool ProtectedInvoke(this Action action, out Exception exception)
- {
- Debug.Assert(action != null);
- try
- {
- action();
- exception = null;
- return true;
- }
- catch (Exception e)
- {
- exception = e;
- return false;
- }
- }
- }
- }
|