forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFuncUtils.cs
More file actions
49 lines (44 loc) · 1.19 KB
/
FuncUtils.cs
File metadata and controls
49 lines (44 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using ServiceStack.Logging;
namespace ServiceStack
{
public static class FuncUtils
{
private static readonly ILog Log = LogManager.GetLogger(typeof(FuncUtils));
/// <summary>
/// Invokes the action provided and returns true if no excpetion was thrown.
/// Otherwise logs the exception and returns false if an exception was thrown.
/// </summary>
/// <param name="action">The action.</param>
/// <returns></returns>
public static bool TryExec(Action action)
{
try
{
action();
return true;
}
catch (Exception ex)
{
Log.Error(ex.Message, ex);
}
return false;
}
public static T TryExec<T>(Func<T> func)
{
return TryExec(func, default(T));
}
public static T TryExec<T>(Func<T> func, T defaultValue)
{
try
{
return func();
}
catch (Exception ex)
{
Log.Error(ex.Message, ex);
}
return default(T);
}
}
}