/// <summary> /// Wrap tryFunc with try-catch /// </summary> /// <param name="tryFunc">Action injected for try block</param> /// <param name="catchAction">Action injected into catch block</param> /// <param name="finallyAction">finally block behavior</param> /// <param name="exceptionPolicyName">Must be config supported</param> /// <param name="rethrow">final catch block behavior</param> /// <param name="returnValueAfterException">Value returned after execution of catch block</param> /// <remarks>Execution of exception policy and rethrow behaviors follows exection of catchAction.</remarks> public TResult Wrap <TResult>(Func <TResult> tryFunc, Action <Exception> catchAction, Action finallyAction, String exceptionPolicyName, Rethrow rethrow, TResult returnValueAfterException = default(TResult)) { if (UpdatePolicyAndRethrowFromUsage) { ExceptionPolicyName = exceptionPolicyName; Rethrow = rethrow; } try { return(tryFunc()); } catch (Exception exc) { catchAction(exc); if (String.IsNullOrEmpty(ExceptionPolicyName) || ExceptionPolicy.HandleException(exc, exceptionPolicyName)) { if (rethrow != Rethrow.No) { switch (rethrow) { case Rethrow.Throw: throw; case Rethrow.ThrowNamed: throw exc; case Rethrow.ThrowNew: throw new Exception("re-throw", exc); } } } } finally { finallyAction(); } return(returnValueAfterException); }
/// <summary> /// Wrap specified actions in try-catch-finally. /// </summary> /// <param name="tryAction">Action injected for try block</param> /// <param name="catchAction">Initial action injected for catch block</param> /// <param name="finallyAction">Action injected for finally block</param> /// <param name="exceptionPolicyName">Must be config supported</param> /// <param name="rethrow">final catch block behavior</param> /// <remarks>Exception policy and rethrow behaviors follow execution of catchAction</remarks> public void Wrap(Action tryAction, Action <Exception> catchAction, Action finallyAction, String exceptionPolicyName, Rethrow rethrow) { if (UpdatePolicyAndRethrowFromUsage) { ExceptionPolicyName = exceptionPolicyName; Rethrow = rethrow; } try { tryAction(); } catch (Exception exc) { catchAction(exc); if (String.IsNullOrEmpty(ExceptionPolicyName) || ExceptionPolicy.HandleException(exc, exceptionPolicyName)) { if (rethrow != Rethrow.No) { switch (rethrow) { case Rethrow.Throw: throw; case Rethrow.ThrowNamed: throw exc; case Rethrow.ThrowNew: throw new Exception("re-throw", exc); } } } } finally { finallyAction(); } }
static void Main() { try { Rethrow.GenException(); } catch (IndexOutOfRangeException) { // recatch exception Console.WriteLine("Fatal error -- " + "program terminated."); } }
static public void Main() { Rethrow rethrow = new Rethrow(); try { rethrow.Foo(); } catch (Exception e) { Console.WriteLine(e.Message); } }
public override void Evaluate() { s.evaluate(); Exception problem = null; try { LuceneTestCase.AssertSaneFieldCaches(d.DisplayName); } catch (Exception t) { problem = t; } FieldCache.DEFAULT.purgeAllCaches(); if (problem != null) { Rethrow.Rethrow(problem); } }
protected internal override Statement VisitRethrow(Rethrow inst) { return(new ThrowStatement()); }
/// <summary> /// Wrap tryAction with try-catch using specified exception policy and rethrow behavior in catch block. /// </summary> /// <param name="tryAction">Action injected for try block</param> /// <param name="exceptionPolicyName">Must be config supported</param> /// <param name="rethrow">final catch block behavior</param> public void Wrap(Action tryAction, String exceptionPolicyName, Rethrow rethrow) { Wrap(tryAction, NoActionExc, NoAction, exceptionPolicyName, rethrow); }
/// <summary> /// Wrap tryFunc with try-catch /// </summary> /// <param name="tryFunc">Action injected for try block</param> /// <param name="catchAction">Action injected into catch block</param> /// <param name="exceptionPolicyName">Must be config supported</param> /// <param name="rethrow">final catch block behavior</param> /// <param name="returnValueAfterException">Value returned after execution of catch block</param> /// <remarks>Execution of exception policy and rethrow behaviors follows exection of catchAction.</remarks> public TResult Wrap <TResult>(Func <TResult> tryFunc, Action <Exception> catchAction, String exceptionPolicyName, Rethrow rethrow, TResult returnValueAfterException = default(TResult)) { return(Wrap(tryFunc, catchAction, NoAction, exceptionPolicyName, rethrow, returnValueAfterException)); }
/// <summary> /// Wrap tryAction with try-catch /// </summary> /// <param name="tryAction">Action injected for try block</param> /// <param name="catchAction">Initial action injected for catch block</param> /// <param name="exceptionPolicyName">Must be config supported</param> /// <param name="rethrow">final catch block behavior</param> /// <remarks>Exception policy and rethrow behaviors defaulted from construction or prior usage. /// Execution of exception policy and rethrow behaviors follow execution of catchAction. /// </remarks> public void Wrap(Action tryAction, Action <Exception> catchAction, String exceptionPolicyName, Rethrow rethrow) { Wrap(tryAction, catchAction, NoAction, exceptionPolicyName, rethrow); }
protected internal override void VisitRethrow(Rethrow inst) { MarkUnreachable(); }
static void Main(string[] args) { //Завдання: Продемонструйте в коді: //1.Приклади виникнення та перехоплення exception (+) //2.Використання сatch без параметрів (+) //3.Повторну генерація exception в блоці catch та спосіб розкрутки стеку(отримати //початкову точку виникнення виключення після повторної генерації виключення) (+) //4.Створіть свій клас для генерації exception та використайте його в коді. (+) //5.Додатково: Наведіть в коді приклади генерації наступних виключень(використовувати throw //заборонено, виключення повинно генеруватись CLR автоматично): // 5.1.NullReferenceException () // 5.2.ArgumentNullException () // 5.3.InvalidOperationException () int x_1 = 1, y_1 = 0, z_1; //1-st task int x_2 = 1, y_2 = 0, z_2; //2-nd task //1 try { z_1 = x_1 / y_1; } catch (DivideByZeroException ex) { Console.WriteLine("Zero division, result = 0"); z_1 = 0; } Console.WriteLine("z = " + z_1); //2 try { z_2 = x_2 / y_2; } catch (Exception) { Console.WriteLine("Zero division, result = 0"); } catch { Console.WriteLine("Alone old catch block"); } //3 try { Rethrow.GenException(); } catch (IndexOutOfRangeException) { // перехватить исключение повторно Console.WriteLine("Fatal error - programm is interrupted."); } //4 try { throw new CustomHandmadeException("Hello World"); } catch (CustomHandmadeException expectedExc) { Console.WriteLine("What did you expect?"); expectedExc.Cure(); } Console.ReadKey(); }