[問題] 防呆寫法
請問關於 C# 防呆 寫法要怎樣比較妥當?
下面四種方法
Funciton 回傳 bool , 最外層再來寫錯誤訊息
或是 string 或 enum 或是自己些個 關於 Error class 代進去
或是 try catch (應該比較不推薦)
寫法讓我困擾滿久的
感謝~
    public enum Error
    {
        Pass, CantOpenFile,
    }
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = @"C:\123.txt";
            //case1
            //用 if + bool 來判斷是否成功 ,
            if (File.checkFile(filePath))
            {
                Console.WriteLine("檔案存在");
            }
            else
            {
                Console.WriteLine("檔案不存在");
            }
            //case2
            // 用 message 丟進去, 再判斷是否成功 , 無回傳 bool
            string message = "";
            File.checkFile(filePath, ref message);
            Console.WriteLine(message);
            //case3
            Error error = Error.Pass;
            File.checkFile(filePath, ref error);
            Console.WriteLine(error.ToString());
            //case4
            try
            {
                //........
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
    class File
    {
        public static bool checkFile(string filePath)
        {
            bool result = System.IO.File.Exists(filePath);
            return result;
        }
        public static void checkFile(string filePath, ref string message)
        {
            if(System.IO.File.Exists(filePath))
            {
                message = "檔案存在";
            }
            else
            {
                message = "檔案不存在";
            }
        }
        public static void checkFile(string filePath, ref Error error)
        {
            if (System.IO.File.Exists(filePath))
            {
                error = Error.Pass;
            }
            else
            {
                error = Error.CantOpenFile;
            }
        }
    }
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 42.73.155.188
※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1538637010.A.F0C.html
推
10/04 16:07, 
                                7年前
                            , 1F
10/04 16:07, 1F
推
10/04 16:29, 
                                7年前
                            , 2F
10/04 16:29, 2F
→
10/04 16:29, 
                                7年前
                            , 3F
10/04 16:29, 3F
我自己來自問自答好了
開檔寫入只是想舉例而已
只是想知道class 裡面到底哪一步有問題, 可以傳到最外層讓UI顯示
與同事討論過後 應該比較像下列
丟出個 Error,再讓外面去顯示
不知道是否有更好建議
    public static class Error
    {
        public static int PASS = 0;
        public static int OPEN_FILE = 1;
        public static int WRITE_FILE = 2;
    }
    class File
    {
        string path = @"C:\\";
        public static int openAndWriteFile(string path)
        {
            if(!File.openFile(path))
                return Error.OPEN_FILE;
            if (!File.writeString("hello"))
                return Error.WRITE_FILE;
            return Error.PASS;
        }
        public static bool openFile(string File)
        {
            return true;
        }
        public static bool writeString(string str)
        {
            return true;
        }
    }
※ 編輯: abc95007 (220.133.187.22), 10/04/2018 23:03:07
推
10/04 23:35, 
                                7年前
                            , 4F
10/04 23:35, 4F
→
10/04 23:35, 
                                7年前
                            , 5F
10/04 23:35, 5F
→
10/04 23:35, 
                                7年前
                            , 6F
10/04 23:35, 6F
→
10/04 23:35, 
                                7年前
                            , 7F
10/04 23:35, 7F
也是從別人那邊 code 學來的
try 似乎比較像是在無法預期會發生什麼錯誤
比較難掌控情況下使用
function 丟出個 bool 出來, 再去判斷
檔案開啟失敗了, 我就知道 Error 是甚麼
檔案寫入失敗了,我就知道 Error 是甚麼
但我還是不太確定對於防呆哪一種比較好, 同時又讓外面 UI 知道到底錯在哪裡
※ 編輯: abc95007 (220.133.187.22), 10/05/2018 00:05:46
推
10/05 02:36, 
                                7年前
                            , 8F
10/05 02:36, 8F
→
10/05 02:36, 
                                7年前
                            , 9F
10/05 02:36, 9F
→
10/05 02:41, 
                                7年前
                            , 10F
10/05 02:41, 10F
→
10/05 02:41, 
                                7年前
                            , 11F
10/05 02:41, 11F
→
10/05 02:41, 
                                7年前
                            , 12F
10/05 02:41, 12F
→
10/05 02:41, 
                                7年前
                            , 13F
10/05 02:41, 13F
→
10/05 02:45, 
                                7年前
                            , 14F
10/05 02:45, 14F
→
10/05 02:45, 
                                7年前
                            , 15F
10/05 02:45, 15F
推
10/05 02:50, 
                                7年前
                            , 16F
10/05 02:50, 16F
→
10/05 02:50, 
                                7年前
                            , 17F
10/05 02:50, 17F
→
10/05 02:50, 
                                7年前
                            , 18F
10/05 02:50, 18F
→
10/05 02:55, 
                                7年前
                            , 19F
10/05 02:55, 19F
→
10/05 02:55, 
                                7年前
                            , 20F
10/05 02:55, 20F
→
10/05 02:55, 
                                7年前
                            , 21F
10/05 02:55, 21F
→
10/05 02:55, 
                                7年前
                            , 22F
10/05 02:55, 22F
→
10/05 02:57, 
                                7年前
                            , 23F
10/05 02:57, 23F
→
10/05 02:57, 
                                7年前
                            , 24F
10/05 02:57, 24F
推
10/05 19:57, 
                                7年前
                            , 25F
10/05 19:57, 25F
→
10/05 19:58, 
                                7年前
                            , 26F
10/05 19:58, 26F
→
10/05 20:00, 
                                7年前
                            , 27F
10/05 20:00, 27F
→
10/05 20:00, 
                                7年前
                            , 28F
10/05 20:00, 28F
→
10/05 20:01, 
                                7年前
                            , 29F
10/05 20:01, 29F
→
10/05 20:03, 
                                7年前
                            , 30F
10/05 20:03, 30F
→
10/05 20:04, 
                                7年前
                            , 31F
10/05 20:04, 31F
→
10/05 20:05, 
                                7年前
                            , 32F
10/05 20:05, 32F
→
10/05 20:06, 
                                7年前
                            , 33F
10/05 20:06, 33F
→
10/05 20:06, 
                                7年前
                            , 34F
10/05 20:06, 34F
→
10/05 21:36, 
                                7年前
                            , 35F
10/05 21:36, 35F
→
10/05 21:37, 
                                7年前
                            , 36F
10/05 21:37, 36F
→
10/05 21:37, 
                                7年前
                            , 37F
10/05 21:37, 37F
→
10/06 00:27, 
                                7年前
                            , 38F
10/06 00:27, 38F
推
10/06 00:45, 
                                7年前
                            , 39F
10/06 00:45, 39F
→
10/06 00:48, 
                                7年前
                            , 40F
10/06 00:48, 40F
推
10/06 00:51, 
                                7年前
                            , 41F
10/06 00:51, 41F
→
10/06 00:58, 
                                7年前
                            , 42F
10/06 00:58, 42F
→
10/06 00:58, 
                                7年前
                            , 43F
10/06 00:58, 43F
→
10/06 00:58, 
                                7年前
                            , 44F
10/06 00:58, 44F
→
10/06 00:58, 
                                7年前
                            , 45F
10/06 00:58, 45F
推
10/06 06:16, 
                                7年前
                            , 46F
10/06 06:16, 46F
→
10/06 06:16, 
                                7年前
                            , 47F
10/06 06:16, 47F
→
10/06 06:16, 
                                7年前
                            , 48F
10/06 06:16, 48F
→
10/06 06:16, 
                                7年前
                            , 49F
10/06 06:16, 49F
→
10/06 06:16, 
                                7年前
                            , 50F
10/06 06:16, 50F
→
10/06 11:05, 
                                7年前
                            , 51F
10/06 11:05, 51F
推
10/09 17:04, 
                                7年前
                            , 52F
10/09 17:04, 52F
→
11/02 21:54, 
                                7年前
                            , 53F
11/02 21:54, 53F
→
11/02 21:54, 
                                7年前
                            , 54F
11/02 21:54, 54F
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章