public void AddCatchVariable(string variableName) { if (Node is IGeneralCatchClause) { if (HasVariable) { return; } if (String.IsNullOrEmpty(variableName)) { variableName = NameFactory.CatchVariableName(Node, CaughtException); } var codeFactory = new CodeElementFactory(GetElementFactory()); var newCatch = codeFactory.CreateSpecificCatchClause(null, Node.Body, variableName); if (newCatch == null) { return; } Node.ReplaceBy(newCatch); Node = newCatch; Variable = new CatchVariableModel(AnalyzeUnit, newCatch.ExceptionDeclaration as ICatchVariableDeclaration); } else { if (HasVariable) { return; } if (String.IsNullOrEmpty(variableName)) { variableName = NameFactory.CatchVariableName(Node, CaughtException); } var specificNode = (ISpecificCatchClause)Node; var exceptionType = (IUserDeclaredTypeUsage)specificNode.ExceptionTypeUsage; var exceptionTypeName = exceptionType.TypeName.NameIdentifier.Name; var tempTry = GetElementFactory().CreateStatement("try {} catch($0 $1) {}", exceptionTypeName, variableName) as ITryStatement; if (tempTry == null) { return; } var tempCatch = tempTry.Catches[0] as ISpecificCatchClause; if (tempCatch == null) { return; } var resultVariable = specificNode.SetExceptionDeclaration(tempCatch.ExceptionDeclaration); Variable = new CatchVariableModel(AnalyzeUnit, resultVariable); } }
/// <summary>Adds a catch clause to the try statement. </summary> /// <param name="exceptionType">The exception type in the added catch clause. </param> public void AddCatchClause(IDeclaredType exceptionType) { var codeElementFactory = new CodeElementFactory(GetElementFactory()); var variableName = NameFactory.CatchVariableName(Node, exceptionType); var catchClauseNode = codeElementFactory.CreateSpecificCatchClause(exceptionType, null, variableName); Node.AddCatchClause(catchClauseNode); }