private static string GetExceptionClause(CatchBlock catchBlock, TranslationContext context) { var exceptionTypeName = catchBlock.Test.GetFriendlyName(); if (ExceptionUsageFinder.IsVariableUsed(catchBlock)) { var filter = (catchBlock.Filter != null) ? " when " + context.Translate(catchBlock.Filter) : null; return($" ({exceptionTypeName} {catchBlock.Variable.Name})" + filter); } return((catchBlock.Test != typeof(Exception)) ? $" ({exceptionTypeName})" : null); }
private static string GetCatchBlock(CatchBlock catchBlock, TranslationContext context) { var catchBody = context.TranslateCodeBlock(catchBlock.Body); var exceptionClause = GetExceptionClause(catchBlock, context); var catchBodyBlock = catchBody.WithCurlyBraces(); if (catchBlock.Variable != null) { catchBodyBlock = catchBodyBlock .Replace($"throw {catchBlock.Variable.Name};", "throw;"); } return($@"catch{exceptionClause}{catchBodyBlock} "); }
public static bool IsVariableUsed(CatchBlock catchHandler) { if (catchHandler.Variable == null) { return(false); } var visitor = new ExceptionUsageFinder(catchHandler); visitor.Visit(catchHandler.Filter); if (!visitor._usageFound) { visitor.Visit(catchHandler.Body); } return(visitor._usageFound); }
private ExceptionUsageFinder(CatchBlock catchHandler) { _catchHandler = catchHandler; }
protected override CatchBlock VisitCatchBlock(CatchBlock @catch) { return(VisitConstruct(@catch, base.VisitCatchBlock)); }