/// <summary>Creates a try-catch block around this block. </summary> /// <param name="exceptionType">The exception type to catch. </param> /// <returns><c>true</c> if the try-catch block could be created; otherwise, <c>false</c>. </returns> public bool SurroundWithTryBlock(IDeclaredType exceptionType) { var codeElementFactory = new CodeElementFactory(GetElementFactory()); var exceptionVariableName = NameFactory.CatchVariableName(Node, exceptionType); var tryStatement = codeElementFactory.CreateTryStatement(exceptionType, exceptionVariableName, Node); var block = codeElementFactory.CreateBlock(Node); tryStatement.SetTry(block); Node.ReplaceBy(tryStatement); return(true); }
/// <summary>Creates a try-catch block around this block. </summary> /// <param name="exceptionType">The exception type to catch. </param> /// <returns><c>true</c> if the try-catch block could be created; otherwise, <c>false</c>. </returns> public bool SurroundWithTryBlock(IDeclaredType exceptionType) { var containingStatement = Node.GetContainingStatement(); if (containingStatement != null && containingStatement.LastChild != null) { var codeElementFactory = new CodeElementFactory(GetElementFactory()); var exceptionVariableName = NameFactory.CatchVariableName(Node, exceptionType); var tryStatement = codeElementFactory.CreateTryStatement(exceptionType, exceptionVariableName, Node); var spaces = GetElementFactory().CreateWhitespaces(Environment.NewLine); LowLevelModificationUtil.AddChildAfter(containingStatement.LastChild, spaces[0]); var block = codeElementFactory.CreateBlock(containingStatement); tryStatement.SetTry(block); containingStatement.ReplaceBy(tryStatement); return(true); } return(false); }