新手提问,望大伙指点(关于添加捕捉异常块)
需要添加一个try块,捕捉异常,总是不能顺利添加,请指教哈
我添加红色字体代码,结果却不一样,请各位师兄帮忙看看问题出在哪,多谢!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MathsOperators
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
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;
}
private int doCalculation(int leftHandSide, int rightHandSide)
{
int result = 0;
if (addition.IsChecked.HasValue && addition.IsChecked.Value)
result = addValues(leftHandSide, rightHandSide);
else if (subtraction.IsChecked.HasValue && subtraction.IsChecked.Value)
result = subtractValues(leftHandSide, rightHandSide);
else if (multiplication.IsChecked.HasValue && multiplication.IsChecked.Value)
result = multiplyValues(leftHandSide, rightHandSide);
else if (division.IsChecked.HasValue && division.IsChecked.Value)
result = divideValues(leftHandSide, rightHandSide);
else if (remainder.IsChecked.HasValue && remainder.IsChecked.Value)
result = remainderValues(leftHandSide, rightHandSide);
return result;
}
private int addValues(int leftHandSide, int rightHandSide)
{
expression.Text = leftHandSide.ToString() + " + " + rightHandSide.ToString();
return leftHandSide + rightHandSide;
}
private int subtractValues(int leftHandSide, int rightHandSide)
{
expression.Text = leftHandSide.ToString() + " - " + rightHandSide.ToString();
return leftHandSide - rightHandSide;
}
private int multiplyValues(int leftHandSide, int rightHandSide)
{
expression.Text = leftHandSide.ToString() + " * " + rightHandSide.ToString();
return leftHandSide * rightHandSide;
}
private int divideValues(int leftHandSide, int rightHandSide)
{
expression.Text = leftHandSide.ToString() + " / " + rightHandSide.ToString();
return leftHandSide / rightHandSide;
}
private int remainderValues(int leftHandSide, int rightHandSide)
{
expression.Text = leftHandSide.ToString() + " % " + rightHandSide.ToString();
return leftHandSide % rightHandSide;
}
private void quitClick(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
[解决办法]
你这个代码有语法错误,正确应该是:
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;
}
}
[解决办法]
catch下面掉了一个 } 吧
[解决办法]
try不是跟函数写在一块的,是跟for差不多的。
类似于这样:
void function()
{
.....
try
{
}
catch(....)
{
}
finally
{
}
.....
}
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;
}
}
private void calculateClick(object sender, RoutedEventArgs e)
{
int leftHandSide,rightHandSide,answer;
try
{
leftHandSide = int.Parse(lhsOperand.Text);
rightHandSide = int.Parse(rhsOperand.Text);
}
catch (FormatException fEx)
{
result.Text=fEx.Message;
return;
}
answer = doCalculation(leftHandSide, rightHandSide);
result.Text = answer.ToString();
}