TAG | try/catch
The prototype javascript library has a class (Try) and function called these(). This function accepts an array of functions as it’s sole argument and it will execute each function in the array, in the order they are added, and return the result from the first function that executes without error. If none of the functions executes successfully undefined is returned.
For example:
var i = Try.These(
function() { return 9 / 0; },
function() { return 1; }
);
If we ran the sample above i would equal 1 because the first function would encounter a division by zero error. However:
var h = 0;
var i = Try.These(
function() { return 9 / h; },
function() { return 1 / h; }
);
i will equal undefined in this example.
With the .Net framework’s generics library we can achieve roughly the same results. We won’t be able to assign an undefined value to the result but we can play around with the default keyword. :D
public class Try
{
public static T These(params Func[] delegates)
{
for (int i = 0; i <= delegates.Length - 1; i++)
{
try
{
return (T)delegates[i]();
}
catch
{
}
}
return default(T);
}
}
So for example:
static void Main(string[] args)
{
int i = 0;
int y = Try.These(
delegate() {
int x = 0;
return i / x;
},
delegate() {
return ++i;
});
Console.Write("y is " + y.ToString());
}
Will output
y is 0
.NET · c# · Programming · prototype · try.these() · try/catch
