首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

新手提问,望大伙指点(关于添加捕捉错误块)

2013-02-24 
新手提问,望大伙指点(关于添加捕捉异常块)需要添加一个try块,捕捉异常,总是不能顺利添加,请指教哈我添加红

新手提问,望大伙指点(关于添加捕捉异常块)
需要添加一个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;
   }
}


那段这种格式。
[解决办法]
引用:
C# code?1234567891011121314 private void calculateClick(object sender, RoutedEventArgs e){  try    {            int leftHandSide = int.Parse(lhsOperand.Text);            int rightHandSide……


这个异常捕获不够严谨,下面2句不会抛出FormatException异常。
int answer = doCalculation(leftHandSide, rightHandSide);
result.Text = answer.ToString();
按楼主的意思应该只是想捕获下面2句的异常信息:
int leftHandSide = int.Parse(lhsOperand.Text);
int rightHandSide = int.Parse(rhsOperand.Text);

建议下面的写法:

 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();
}


[解决办法]
function(){
try{
}
catch(Exception ex)
{
}
}
这种模式。

热点排行