mscorlib中未处理的异常

时间:2012-01-27 17:57:47

标签: c# wcf exception console

发现了WCF并因此开始探索它。 这是一个C#控制台应用程序。代码工作正常,除非我尝试提取。如果我输入错误类型的数量它会检测到(捕获),通知我输入无效并将我发回菜单提示。这是好的和花花公子,直到我到达我试图撤回比我更平衡的部分(平衡)。据说,我应该得到消息说我缺少资金以便撤回这么多。相反,我得到了这个:

  

mscorlib.dll中发生未处理的“System.FormatException”类型异常

我哪里出错了?

主要

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace BankAccountClient
{
    class Program
    {
        static void Main(string[] args)
        {
            BankAccountClient.ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();
            bool done = false;
            do
            {
                Console.WriteLine("Select one of the following:");
                Console.WriteLine("\t1 -- Withdraw");
                Console.WriteLine("\t2 -- Deposit");
                Console.WriteLine("\t3 -- Balance");
                Console.Write("Enter Your selection (0 to exit): ");
                string strSelection = Console.ReadLine();
                int iSel;
                try
                {
                    iSel = int.Parse(strSelection);
                }
                catch (FormatException)
                {
                    Console.WriteLine("\r\nWhat?\r\n");
                    continue;
                }
                Console.WriteLine("\nYou selected  " + iSel + "\n");
                switch (iSel)
                {
                    case 0:
                        done = true;
                        break;
                    case 1:
                        int balance = client.Balance();
                        int amount;

                        //WCF Withdraw
                        Console.Write("How much would you like to withdraw?: ");
                        try
                        {
                            amount = int.Parse(Console.ReadLine());
                        }
                        catch (FormatException)
                        {
                            Console.WriteLine("\r\nInvalid input. Must be an integer\r\n");
                            continue;
                        }
                        if (amount > balance)
                        {
                            Console.WriteLine(String.Format("\r\nNot enough funds to withdraw ${0}\r\n"), amount);
                            continue;
                        }
                        else
                            client.Withdraw(amount);
                        Console.WriteLine(String.Format("\nCurrent balance is ${0}\n", client.Balance()));
                        break;
                    case 2:
                        //WCF Deposit
                        Console.WriteLine("Deposit();");
                        break;
                    case 3:
                        //WCF Balance
                        Console.WriteLine(String.Format("Current balance is ${0}", client.Balance()));
                        break;
                    default:
                        Console.WriteLine("You selected an invalid number: {0}\r\n", iSel);
                        continue;
                }
                Console.WriteLine();
            } while (!done);

            Console.WriteLine("\nGoodbye!");
        }
    }
}

WCF服务(简称)

public class Service : IService
{

    private static int balance;

    public void Withdraw(int value)
    {
        balance -= value;
    }

    public void Deposit(int value)
    {
        balance += value;
    }

    public int Balance()
    {
        return balance;
    }
}

2 个答案:

答案 0 :(得分:2)

您需要将金额移入此行的String.Format方法

Console.WriteLine(String.Format("\r\nNot enough funds to withdraw ${0}\r\n"), amount);

所以

Console.WriteLine(String.Format("\r\nNot enough funds to withdraw ${0}\r\n", amount));

答案 1 :(得分:1)

您已将括号放在错误的位置。将输出更改为:

Console.WriteLine(String.Format("\r\nNot enough funds to withdraw {0}\r\n", amount));