I wrote two functions which look similar, how can I optimize them?
Note:
1.AsyncCompletedEventArgs is the base class of DownloadStringCompletedEventArg and UploadStringCompletedEventArgs.
2.Result property is not in the AsyncCompletedEventArgs.
3.DownloadStringCompletedEventArgs has a Error property, if Error is null, then try to access Result property, the exception occurs.
void fun1(DownloadStringCompletedEventArgs e)
{
try
{
string s = e.Result;
}
catch (WebException eX)
{
HandleWebException();
}
}
void fun2(UploadStringCompletedEventArgs e)
{
try
{
string s = e.Result;
}
catch (WebException eX)
{
HandleWebException();
}
}
You code may be changed to something like below:
void fun1(DownloadStringCompletedEventArgs e) { Process(e); }
void fun2(UploadStringCompletedEventArgs e) { Process(e); }
private void Process(dynamic eventArgs)
{
try
{
string s = eventArgs.Result;
}
catch (WebException e)
{
HandleWebException(e);
}
}
UploadStringCompletedEventArgs and DownloadCompletedEventArgs both extend AsyncCompletedEventArgs but unfortunately the base class does not define the Result property.
A TryX pattern with a result accessor delegate might be appropriate here:
public bool TryGetResult(Func<string> resultAccessor, out string result)
{
try
{
result = resultAccessor();
return true;
}
catch(WebException)
{
HandleWebException();
result = null;
return false;
}
}
void fun1(DownloadStringCompletedEventArgs e)
{
string result;
if (TryGetResult(() => e.Result, out result))
{
// Success
}
}
void fun2(UploadStringCompletedEventArgs e)
{
string result;
if (TryGetResult(() => e.Result, out result))
{
// Success
}
}
I'd recommend trying to work in a check to AsyncCompletedEventArgs.Error, though, as exceptions are quite expensive.
Something like this:
void fun1(DownloadStringCompletedEventArgs e)
{
var result = Process<string>(e);
if (result != null)
{
// TODO your logic here
}
}
void fun2(UploadStringCompletedEventArgs e)
{
var result = Process<string>(e);
if (result != null)
{
// TODO your logic here
}
}
private T Process<T>(AsyncCompletedEventArgs result)
{
if (result.Error != null)
HandleWebException(result.Error);
else if (!result.Cancelled)
{
//var prop = result.GetType().GetProperty("Result");
//return (T) prop.GetValue(result, null);
return (T) ((dynamic)result).Result;
}
//else // TODO handle cancelled
return default(T);
}
Maybe you could write a function that takes a parameter of type AsyncCompletedEventArgs (from which both eventArg classes you use inherit) and then attempt to cast it to the correct type in your code. That would allow you to complete both in the same method, but looking at your code it probably wouldn't have much benefit for you. Good luck!
Related
I have a program that calls dozens of methods with varying signatures, but the exception handling inside each one is identical. Is there some way to define a method that can accept a reference to a generic method with various signatures (which rules out a Delegate - right?) and return the object, or void that the method requires? I'm using .NET 4.72.
Here is stripped down version of what I'm currently doing and some pseudo-code of what I'd like to do:
static class StackOverflowQuestion
{
public static void Main(string[] args)
{
// What I'm currently doing:
MethodOne("x");
int ret = MethodTwo(0);
//.
//.
//.
MethodNineteen();
// what I'd like to do is replace MethodOne(), MethodTwo(), ..., Method Nineteen()
// with something like:
RunMethod<void>(MethodOneWork, new object[] {"x"});
ret = RunMethod<int>(MethodTwoWork, new object []{1});
//.
//.
//.
RunMethod<void>(MethodNineteenWork, null);
}
private static void MethodOne(string st)
{
try
{
// the try clause is the only difference between the methods
MethodOneWork(st);
}
catch (MyExceptionA)
{
HandleExceptionA();
return;
}
catch(MyExceptionB)
{
HandleExceptionB();
}
catch(Exception)
{
HandleGenericException();
}
}
private static int MethodTwo(int v)
{
try
{
return MethodTwoWork(v);
}
catch (MyExceptionA)
{
HandleExceptionA();
return -1;
}
catch (MyExceptionB)
{
HandleExceptionB();
return -2;
}
catch(Exception)
{
HandleGenericException();
return 0;
}
}
private static void MethodNineteen()
{
try
{
MethodNineteenWork();
}
catch (MyExceptionA)
{
HandleExceptionA();
return;
}
catch (MyExceptionB)
{
HandleExceptionB();
}
catch(Exception)
{
HandleGenericException();
}
}
/// <summary>
/// Run generic method with generic signature
/// </summary>
private static <T> RunMethod(Delegate MethodxWork, object[] myParams)
{
try
{
new <T>() retVal = MethodxWork(myParams);
return retVal;
}
catch (MyExceptionA)
{
HandleExceptionA();
return new <T>();
}
catch (MyExceptionB)
{
HandleExceptionB();
return new <T>();
}
catch(Exception)
{
HandleGenericException();
return new <T>();
}
}
private static void HandleExceptionB()
{
//handle it
}
private static void HandleExceptionA()
{
//handle it
}
private static void HandleGenericException()
{
//handle it
}
}
internal class MyExceptionB : Exception
{
}
internal class MyExceptionA : Exception
{
}
Sure, just create a few methods whose job it is to handle the exceptions, one for returning results and the other for void, and provide something that does your work.
T Handle<T>(Func<T> call)
{
try
{
return call();
}
catch(YourException ex)
{
return default;
}
}
void Handle(Action call)
{
try
{
call();
}
catch(YourException ex)
{
}
}
After that, you can call your other methods with varying signatures inside there.
var result = Handle(() => SomeCallWithVaryingSignature(...));
Handle(() => SomeOtherCall(...));
Let say we have something like:
public CustomType DoSomething1() {
CustomType ct = new CustomType();
try {
// do something
}
catch(Exception e) {
ct.Passed = false;
}
return ct;
}
public CustomType DoSomething2() {
CustomType ct = new CustomType();
try {
// do something
}
catch(Exception e) {
ct.Passed = false;
}
return ct;
}
public CustomType DoSomething3() {
CustomType ct = new CustomType();
try {
// do something
}
catch(Exception e) {
ct.Passed = false;
}
return ct;
}
These methods are executed by another program using reflection, and if CustomType property Passed == false, program stop executing another. It's due to architecture aspects.
Is it possible to create some attribute or something like that to avoid using try catches, so that if exception is thrown in method it will make Passed property as false and return to program? E.g.
[CatchException('Passed', false)]
public CustomType DoSomething1() {
CustomType ct = new CustomType();
// do something
return ct;
}
And if in process of 'do something' error would be thrown ct.Passed will be equal to 'false'
You can do the following:
public static T SafeProcessing<T>(Action<T> action, Action<T> onFail)
where T: new()
{
var t = new T();
try
{
a(t);
}
catch (Exception e)
{
//Log e
onFail(t);
}
return t;
}
And no you'd use it like this:
return SafeProcessing(c => DoSomething(c), c => c.Safe = false);
If I understand your question correct, you want to avoid repeating the try-catch-block. You can solve this by creating a function you are passing the logic you want to deal with.
public static CustomType CatchException(Action a)
{
CustomType ct = new CustomType();
try
{
a();
}
catch
{
ct.Passed = false;
}
return ct;
}
Now you can simply call the function with any logic you need like multiple times in a very comfortable way.
public CustomType DoSomething1()
{
return CatchException(() =>
{
//Do something
});
}
...
I have a lot of functions that look like this:
public void DoParticularThing(RecurringTaskRunResult result) {
try {
// Do a bunch of stuff
}
catch (Exception e) {
result.Succeeded = false;
result.Results += e.ToString();
db.SaveChanges();
}
}
So I decided to extract this out in the name of DRY code:
public void RunThing(Action<RecurringTaskRunResult> action, RecurringTaskRunResult result) {
try {
action(result);
}
catch (Exception e) {
result.Succeeded = false;
result.Results += e.ToString();
db.SaveChanges();
}
}
This way I can call DoParticularThing like this:
RunThing(DoParticularThing, result);
But some of my functions also accept another parameter:
public void DoOtherParticularThing(RecurringTaskRunResult result, List<string> strings) {
try {
// Do a bunch of stuff
}
catch (Exception e) {
result.Succeeded = false;
result.Results += e.ToString();
db.SaveChanges();
}
}
How can I modify RunThing to optionally accept another parameter?
Perhaps this :
public void RunThing(Action action, RecurringTaskRunResult result) {
try {
action();
}
catch (Exception e) {
result.Succeeded = false;
result.Results += e.ToString();
db.SaveChanges();
}
}
RunThing(() => DoParticularThing(result), result);
RunThing(() => DoSomethingElse(result, list), result);
You can use params accept any parameters in your function.
public void RunThing(Action<RecurringTaskRunResult> action, RecurringTaskRunResult result, params object[] list) {
try {
action(result);
foreach(var item in list)
{
// Do action with your additional parameter
}
}
catch (Exception e) {
result.Succeeded = false;
result.Results += e.ToString();
db.SaveChanges();
}
}
It looks like in all cases you want RunThing to have access to the RecurringTaskRunResult so that you can set some fields on it, but that you don't need access to any of the other parameters. You could try something like this:
void Action1(RecurringTaskRunResult result) { }
void Action2(RecurringTaskRunResult result, object foo) { }
RecurringTaskRunResult result = ...;
object foo = ...;
RunThing(Action1, result);
RunThing(res => Action2(res, foo), result);
So I have the following block of code inside a method: (all variables are local)
// ...
try
{
if (postXml != null)
using (StreamWriter writer = new StreamWriter(req.GetRequestStream()))
writer.Write(postXml.ToString());
}
catch (WebException ex)
{
HttpWebResponse response = ex.Response as HttpWebResponse;
if (response != null)
result = HandleOtherResponse(response, out status);
else result = HandleBadResponse(ex.ToString(), out status);
}
catch (Exception ex)
{
result = HandleBadResponse(ex.ToString(), out status);
}
if (result == null)
{
try
{
HttpWebResponse response = req.GetResponse() as HttpWebResponse;
result = HandleOtherResponse(response, out status);
}
catch (WebException ex)
{
HttpWebResponse response = ex.Response as HttpWebResponse;
if (response != null)
result = HandleOtherResponse(response, out status);
else result = HandleBadResponse(ex.ToString(), out status);
}
catch (Exception ex)
{
result = HandleBadResponse(ex.ToString(), out status);
}
}
// ...
As you can see, the two try statements are different, but the two sets of catch statements are exactly the same. I've been trying to think of a way that it might be possible to not repeat myself here, but I haven't really thought of a way that wouldn't be significantly slower or just as terrible looking. Wondering if anyone has any ideas.
One way would be to write a "safe" invocation method and pass a func to it:
public T SafeInvocation(Func<T> myMethod)
{
T result = default(T);
try
{
// Invoke method
result = myMethod();
}
catch
{
// Do your common catch here
}
return result;
}
Build an additional overload for Action<T> so that you don't need to have a return type. Then you could invoke it elsewhere, passing methods to your method as arguments (Inception?):
SafeInvocation(() =>
{
if (postXml != null)
using (StreamWriter writer = new StreamWriter(req.GetRequestStream()))
writer.Write(postXml.ToString());
}
You could pass an Action into a function that handles the exceptions:
private void HandleErrorsFor(Action action)
{
try
{
action();
}
catch (Exception ex)
{
//repeated exception handling...
{
}
//...
public void DoSomething()
{
HandleErrorsFor(() => {
//try block #1
});
HandleErrorsFor(() => {
//try block #2
});
}
It's a bit easier to read and avoids the repeated code.
You can do something with delegates and cover both try and catch blocks:
static class Program
{
delegate void CodeBlock();
internal delegate void ExceptionCatcher(Exception ex);
private static void Main()
{
CodeBlock b = () => { Console.WriteLine("HELLO WORLD"); };
CodeBlock error = () => { throw new Exception("Exception thrown"); };
ExceptionCatcher silence = exception => { };
ExceptionCatcher e = exception =>
{
var currentColor = Console.BackgroundColor;
Console.BackgroundColor = ConsoleColor.Red;
Console.WriteLine(exception.Message);
Console.BackgroundColor = currentColor;
};
DRYRunner(b, e);
DRYRunner(error , e);
DRYRunner(error , silence);
Console.ReadLine();
}
static void DRYRunner (CodeBlock block, ExceptionCatcher catcher)
{
try
{
block.Invoke();
}
catch (Exception ex)
{
catcher(ex);
}
}
}
edit: Extending this out, we can create a class to help contain and associate code blocks with their possible exceptions and handlers. You could even create a class of common exception handlers and reference them accordingly, mixing them in with ad-hoc handlers:
class ExceptionHandledDelegate
{
public delegate void CodeBlock();
public delegate void ExceptionCatcher(Exception ex);
public Dictionary<Type, ExceptionCatcher> ExceptionHandlers;
public CodeBlock codeBlock { get; set; }
public void Run()
{
try
{
codeBlock.Invoke();
}
catch (Exception ex)
{
var mn = ex.GetType();
if (ExceptionHandlers.Keys.Contains(mn))
{
ExceptionHandlers[mn](ex);
}
else throw;
}
}
}
class CommonHandlers
{
public static void ArgumentHandler(Exception ex)
{
Console.WriteLine("Handling an argument exception");
}
public static void DivZeroHandler(Exception ex)
{
Console.WriteLine("Please don't divide by zero. It upsets the universe.");
}
}
static class Program
{
private static void Main()
{
var ehd = new ExceptionHandledDelegate
{
codeBlock = () => { throw new ArgumentException("An argument exception has been thrown"); },
ExceptionHandlers = new Dictionary<Type, ExceptionHandledDelegate.ExceptionCatcher>
{
{typeof (ArgumentException), CommonHandlers.ArgumentHandler},
{typeof (DivideByZeroException ),CommonHandlers.DivZeroHandler},
{typeof (Exception), exception => Console.WriteLine("An exception has been thrown")}
}
};
ehd.Run();
ehd.codeBlock = () => { throw new Exception("An exception has been thrown"); };
ehd.Run();
ehd.codeBlock = () =>{var denom = 0; Console.WriteLine(100 / denom);};
ehd.Run();
Console.ReadLine();
}
}
I am inserting different message based on the exception type.
I want to insert different custom message into the exception table based on the exception type. I cant use switch statements with exception object.
Any suggestion on how I can do this?
private void ExceptionEngine(Exception e)
{
if (e.)
{
exceptionTable.Rows.Add(null, e.GetType().ToString(), e.Message);
}
if (e is NullReferenceException)
{
...
}
else if (e is ArgumentNullException)
{
...
}
else if (e is SomeCustomException)
{
...
}
else
{
...
}
and inside those if clauses you can cast e to the corresponding exception type to retrieve some specific properties of this exception: ((SomeCustomException)e).SomeCustomProperty
If all the code will be in the if/else blocks then better to use multiple catch (remember to put the most specific types first):
try {
...
} catch (ArgumentNullException e) {
...
} catch (ArgumentException e) { // More specific, this is base type for ArgumentNullException
...
} catch (MyBusinessProcessException e) {
...
} catch (Exception e) { // This needs to be last
...
}
I cant use switch statements with exception object.
If you want to use a switch, you could always use the Typename:
switch (e.GetType().Name)
{
case "ArgumentException" : ... ;
}
This has the possible advantage that you do not match subtypes.
You could unify the handling of different exception types (known at compile time) by a pre-defined dictionary. For example:
// Maps to just String, but you could create and return whatever types you require...
public static class ExceptionProcessor {
static Dictionary<System.Type, Func<String, Exception> sExDictionary =
new Dictionary<System.Type, Func<String, Exception> {
{
typeof(System.Exception), _ => {
return _.GetType().ToString();
}
},
{
typeof(CustomException), _ => {
CustomException tTmp = (CustomException)_;
return tTmp.GetType().ToString() + tTmp.CustomMessage;
}
}
}
public System.String GetInfo(System.Exception pEx) {
return sExDictionary[pEx.GetType()](pEx);
}
}
Usage:
private void ExceptionEngine(Exception e) {
exceptionTable.AddRow(ExceptionProcessor.GetInfo(e));
}