本人初学者,正在学习VC中,使用的软件是Microsoft Visual Studio 2008 操作平台是XP 。现在自己学习到用try/catch语句处理异常。但是发现我按照书上来添加try和catch代码后,出错了。
原来部分代码如下:
private void calculateClick(object sender, RoutedEventArgs e)
{
int leftHandSide = int.Parse(lhsOperand.Text);
int rightHandSide = int.Parse(rhsOperand.Text);
int answer = doCalculation(leftHandSide, rightHandSide);
result.Text = answer.ToString();
} 然后我添加try和catch后的代码如下:
private void calculateClick(object sender, RoutedEventArgs e)
try
{
int leftHandSide = int.Parse(lhsOperand.Text);
int rightHandSide = int.Parse(rhsOperand.Text);
int answer = doCalculation(leftHandSide, rightHandSide);
result.Text = answer.ToString();
}
catch (FormatException fEx)
{
result.Text = fEx.Message;
}
然后在VS2008里面调试运行,就提示发生错误。
错误提示:
错误 2 类、结构或接口成员声明中的标记“try”无效 D:\我的文档\Microsoft Press\Visual CSharp Step By Step\Chapter 6\MathsOperators\MathsOperators\Window1.xaml.cs 31 9 MathsOperators错误 3 类、结构或接口成员声明中的标记“=”无效 D:\我的文档\Microsoft Press\Visual CSharp Step By Step\Chapter 6\MathsOperators\MathsOperators\Window1.xaml.cs 36 25 MathsOperators错误 4 类、结构或接口成员声明中的标记“(”无效 D:\我的文档\Microsoft Press\Visual CSharp Step By Step\Chapter 6\MathsOperators\MathsOperators\Window1.xaml.cs 36 42 MathsOperators错误 5 命名空间并不直接包含诸如字段或方法之类的成员 D:\我的文档\Microsoft Press\Visual CSharp Step By Step\Chapter 6\MathsOperators\MathsOperators\Window1.xaml.cs 38 9 MathsOperators错误 6 应输入 class、delegate、enum、interface 或 struct D:\我的文档\Microsoft Press\Visual CSharp Step By Step\Chapter 6\MathsOperators\MathsOperators\Window1.xaml.cs 43 17 MathsOperators
错误 7-错误 13都是和错误 6 一样的提示。错误 14 应输入类型、命名空间定义或文件尾 D:\我的文档\Microsoft Press\Visual CSharp Step By Step\Chapter 6\MathsOperators\MathsOperators\Window1.xaml.cs 96 5 MathsOperators
这是为什么呢????我都是按照书上来写的啊??难道格式有问题??还是哪里我弄错了???希望老鸟帮个忙!!
原来部分代码如下:
private void calculateClick(object sender, RoutedEventArgs e)
{
int leftHandSide = int.Parse(lhsOperand.Text);
int rightHandSide = int.Parse(rhsOperand.Text);
int answer = doCalculation(leftHandSide, rightHandSide);
result.Text = answer.ToString();
} 然后我添加try和catch后的代码如下:
private void calculateClick(object sender, RoutedEventArgs e)
try
{
int leftHandSide = int.Parse(lhsOperand.Text);
int rightHandSide = int.Parse(rhsOperand.Text);
int answer = doCalculation(leftHandSide, rightHandSide);
result.Text = answer.ToString();
}
catch (FormatException fEx)
{
result.Text = fEx.Message;
}
然后在VS2008里面调试运行,就提示发生错误。
错误提示:
错误 2 类、结构或接口成员声明中的标记“try”无效 D:\我的文档\Microsoft Press\Visual CSharp Step By Step\Chapter 6\MathsOperators\MathsOperators\Window1.xaml.cs 31 9 MathsOperators错误 3 类、结构或接口成员声明中的标记“=”无效 D:\我的文档\Microsoft Press\Visual CSharp Step By Step\Chapter 6\MathsOperators\MathsOperators\Window1.xaml.cs 36 25 MathsOperators错误 4 类、结构或接口成员声明中的标记“(”无效 D:\我的文档\Microsoft Press\Visual CSharp Step By Step\Chapter 6\MathsOperators\MathsOperators\Window1.xaml.cs 36 42 MathsOperators错误 5 命名空间并不直接包含诸如字段或方法之类的成员 D:\我的文档\Microsoft Press\Visual CSharp Step By Step\Chapter 6\MathsOperators\MathsOperators\Window1.xaml.cs 38 9 MathsOperators错误 6 应输入 class、delegate、enum、interface 或 struct D:\我的文档\Microsoft Press\Visual CSharp Step By Step\Chapter 6\MathsOperators\MathsOperators\Window1.xaml.cs 43 17 MathsOperators
错误 7-错误 13都是和错误 6 一样的提示。错误 14 应输入类型、命名空间定义或文件尾 D:\我的文档\Microsoft Press\Visual CSharp Step By Step\Chapter 6\MathsOperators\MathsOperators\Window1.xaml.cs 96 5 MathsOperators
这是为什么呢????我都是按照书上来写的啊??难道格式有问题??还是哪里我弄错了???希望老鸟帮个忙!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货