Reputation: 2492
Good day!
i write simple console program. It have func Main () and class Adapter;
some simple code that explains how it works:
void Main()
{
try
{
Work(array);
//subcribing at some events;
Application.Run();
}
catch(Exception ex)
{
//write to log;
}
}
class Adapter
{
....
public void GetSomething()
{
try
{
...some work and exception goes here;
}
catch(Exception ex)
{
//catch exception and do Work:
Work(array);
}
}
}
When exception goes- it catches at GetSomething. So,i write some values.But i need that the program still running after exception. But after catch in GetSomething method it goes to Exception ex at Main func and program exits;
How to do that program will still running after catch exception in GetSomething method? Thank you!
Upvotes: 2
Views: 17116
Reputation: 74197
If what you want is to catch an exception and continue execution at the point of failure (which might be several layers down in the call stack) and possibly retrying the failed statement, you're pretty much out of luck.
Once your catch
clause is invoked, the stack has been unwound up to that point. You can deal with the exception in some way and then choose zero or one of
throw ;
throw e;
throw new SomeException();
If didn't choose one of the above, execution continues at the point following the try/catch/finally
block. For example:
try
{
DoSomethingThatMightThrowAnException() ;
}
catch ( Exception e )
{
DoSomethingToHandleTheExceptionHere() ;
// Then, choose zero or one of the following:
throw ; // current exception is continue with original stack trace
throw e ; // current exception is re-thrown with new stack trace originating here
throw new Exception() ; // a new exception is thrown with its stack trace originating here
throw new Exception(e) ; // a new exception is thrown as above, with the original exception as the inner exception
}
finally
{
// regardless of whether or not an exception was thrown,
// code in the finally block is always executed after the try
// (and the catch block, if it was invoked)
}
// if you didn't throw, execution continues at this point.
If you don't do one of the above, execution continues at the statement following the try/catch/finally
block.
The best you can do as far as retrying is something like this:
// retry operation at most 3
int number_of_retries = 5 ;
for ( int i = 0 ; i < number_of_retries ; ++i )
{
try
{
DoSomethingThatMightThrowAnException() ;
break ; // exit the loop on success
}
catch( Exception e )
{
Log.Error("well...that didn't work") ;
ExecuteRecoveryCode() ;
}
}
Upvotes: 5
Reputation: 10708
This behavior is not possible in .NET. When an exception is thrown, control exits the current point and continues onto the first catch
statement it can find in the call stack, or exits the program, thus eliminating execution of the call stack up to that point. This is part of the definition of exceptional behavior.
You can instead break down the process being done by Work
and process each item one at a time to enable the same effect as what you're asking. In other words, instead of Work(array)
, try
foreach(var item in array)
{
try { WorkSingle(item); }
catch { continue; }
}
Upvotes: 3
Reputation: 67
You can use try, catch and finally:
http://www.dotnetperls.com/finally
http://msdn.microsoft.com/es-es/library/dszsf989.aspx
Upvotes: 2