簡體   English   中英

如何使用 switch case 處理異常

[英]How to handle exceptions with switch case

在一個項目上工作,這里是 c# 的新項目,我正在嘗試處理未處理的異常。 我想做的是,每當用戶鍵入的內容不是其中一種選擇時,就向用戶提供一條有用的錯誤消息,並不斷提示他們,直到他們輸入有效的響應。

        string input = Console.ReadLine();
    //    bool userBool = false;
    //    while( userBool){
           
    //    }
    
        switch (Int32.Parse(input))
        {
            
            case 1:
                farm.AddGrazingField(new GrazingField());
                Console.WriteLine("Your Facility has been added");
                break;
            case 2:
                farm.AddPlowedField(new PlowedField());
                Console.WriteLine("Your Facility has been added");
                break;
            case 3:
                farm.AddNaturalField(new NaturalField());
                Console.WriteLine("Your Facility has been added");
                break;
            case 4:
                farm.AddChickenHouse(new ChickenHouse());
                Console.WriteLine("Your Facility has been added");
                break;
            case 5:
                farm.AddDuckHouse(new DuckHouse());
                Console.WriteLine("Your Facility has been added");
                break;
            default:
                break;
        }

我知道我可以用 while 循環和條件來做到這一點,但是用 switch case 做這件事還沒有成功。

您可以使用 function 來讀取和驗證用戶的輸入:

int GetUserInput()
{
    while (true)
    {
        Console.Write("Please enter a number: ");
        var input = Console.ReadLine();
        if (int.TryParse(input, out var value))
            return value;
    }
}

如果用戶輸入無效值,此 function 不會拋出異常。 相反,它會再次提示用戶。 您可以擴展它以限制您允許的輸入范圍並編寫描述性錯誤消息。

請注意代碼中的以下更新。

      try
   
        {
            
            Console.WriteLine("Please enter your input in number form");

            string input = Console.ReadLine();

            switch (Int32.Parse(input))
            {

                case 1:
                    farm.AddGrazingField(new GrazingField());
                    Console.WriteLine("Your Facility has been added");
                    break;
                case 2:
                    farm.AddPlowedField(new PlowedField());
                    Console.WriteLine("Your Facility has been added");
                    break;
                case 3:
                    farm.AddNaturalField(new NaturalField());
                    Console.WriteLine("Your Facility has been added");
                    break;
                case 4:
                    farm.AddChickenHouse(new ChickenHouse());
                    Console.WriteLine("Your Facility has been added");
                    break;
                case 5:
                    farm.AddDuckHouse(new DuckHouse());
                    Console.WriteLine("Your Facility has been added");
                    break;
                default:
                    throw new Exception();
            }
        }
        catch (Exception ex)
        {
            //Can proovide more info to user to make sure he knows the input to be entered or options available to him
            Console.WriteLine("Please verify the input");
        }
        finally
        {
            //As it will get executed in case of exception also you can log some message or perform something here
        }

這僅僅是個開始。 您可以擁有自定義異常並將它們用於記錄消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM