public void TryCatchThrow()
{
var cd = new CodeTypeDeclaration();
cd.Name = "Test";
cd.IsClass = true;
// try catch statement with just finally
var cmm = new CodeMemberMethod();
cmm.Name = "FirstScenario";
cmm.ReturnType = new CodeTypeReference(typeof(int));
cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression(typeof(int), "a");
cmm.Parameters.Add(param);
CodeTryCatchFinallyStatement tcfstmt = new CodeTryCatchFinallyStatement();
tcfstmt.FinallyStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("a"), new
CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("a"), CodeBinaryOperatorType.Add,
new CodePrimitiveExpression(5))));
cmm.Statements.Add(tcfstmt);
cmm.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("a")));
cd.Members.Add(cmm);
CodeBinaryOperatorExpression cboExpression = new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("a"), CodeBinaryOperatorType.Divide, new CodeVariableReferenceExpression("a"));
CodeAssignStatement assignStatement = new CodeAssignStatement(new CodeVariableReferenceExpression("a"), cboExpression);
// try catch statement with just catch
cmm = new CodeMemberMethod();
cmm.Name = "SecondScenario";
cmm.ReturnType = new CodeTypeReference(typeof(int));
cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
param = new CodeParameterDeclarationExpression(typeof(int), "a");
cmm.Parameters.Add(param);
cmm.Parameters.Add(new CodeParameterDeclarationExpression(typeof(String), "exceptionMessage"));
tcfstmt = new CodeTryCatchFinallyStatement();
CodeCatchClause catchClause = new CodeCatchClause("e");
tcfstmt.TryStatements.Add(assignStatement);
catchClause.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("a"),
new CodePrimitiveExpression(3)));
catchClause.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("exceptionMessage"),
new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("e"), "ToString")));
tcfstmt.CatchClauses.Add(catchClause);
tcfstmt.FinallyStatements.Add(CreateVariableIncrementExpression("a", 1));
cmm.Statements.Add(tcfstmt);
cmm.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("a")));
cd.Members.Add(cmm);
// try catch statement with multiple catches
cmm = new CodeMemberMethod();
cmm.Name = "ThirdScenario";
cmm.ReturnType = new CodeTypeReference(typeof(int));
cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
param = new CodeParameterDeclarationExpression(typeof(int), "a");
cmm.Parameters.Add(param);
cmm.Parameters.Add(new CodeParameterDeclarationExpression(typeof(String), "exceptionMessage"));
tcfstmt = new CodeTryCatchFinallyStatement();
catchClause = new CodeCatchClause("e", new CodeTypeReference(typeof(ArgumentNullException)));
tcfstmt.TryStatements.Add(assignStatement);
catchClause.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("a"),
new CodePrimitiveExpression(9)));
catchClause.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("exceptionMessage"),
new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("e"), "ToString")));
tcfstmt.CatchClauses.Add(catchClause);
// add a second catch clause
catchClause = new CodeCatchClause("f", new CodeTypeReference(typeof(Exception)));
catchClause.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("exceptionMessage"),
new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("f"), "ToString")));
catchClause.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("a"),
new CodePrimitiveExpression(9)));
tcfstmt.CatchClauses.Add(catchClause);
cmm.Statements.Add(tcfstmt);
cmm.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("a")));
cd.Members.Add(cmm);
// catch throws exception
cmm = new CodeMemberMethod();
cmm.Name = "FourthScenario";
cmm.ReturnType = new CodeTypeReference(typeof(int));
cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
param = new CodeParameterDeclarationExpression(typeof(int), "a");
cmm.Parameters.Add(param);
tcfstmt = new CodeTryCatchFinallyStatement();
catchClause = new CodeCatchClause("e");
tcfstmt.TryStatements.Add(assignStatement);
catchClause.Statements.Add(new CodeCommentStatement("Error handling"));
catchClause.Statements.Add(new CodeThrowExceptionStatement(new CodeArgumentReferenceExpression("e")));
tcfstmt.CatchClauses.Add(catchClause);
cmm.Statements.Add(tcfstmt);
cmm.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("a")));
cd.Members.Add(cmm);
AssertEqual(cd,
@"public class Test {
public static int FirstScenario(int a) {
try {
}
finally {
a = (a + 5);
}
return a;
}
public static int SecondScenario(int a, string exceptionMessage) {
try {
a = (a / a);
}
catch (System.Exception e) {
a = 3;
exceptionMessage = e.ToString();
}
finally {
a = (a + 1);
}
return a;
}
public static int ThirdScenario(int a, string exceptionMessage) {
try {
a = (a / a);
}
catch (System.ArgumentNullException e) {
a = 9;
exceptionMessage = e.ToString();
}
catch (System.Exception f) {
exceptionMessage = f.ToString();
a = 9;
}
return a;
}
public static int FourthScenario(int a) {
try {
a = (a / a);
}
catch (System.Exception e) {
// Error handling
throw e;
}
return a;
}
}");
}