2007年9月2日 星期日

.NET Exception Handling

在寫程式的時候,難免會有錯誤,因此不但要code defensively,還要確定在錯誤產生,使程式無法正常進行時,能夠保留足夠資訊以便進行debug,
最常見的error handling technique就是使用Exception,在try + catch block裡面catch exception之後,可以進行exception log,或是,直接rethrow exception

PS: For the following examples, the error is caused by an overflow after the user clicks on a "Save" button

1. throw ne Exception("錯誤訊息:" + e.Message);
cannot preserve information regarding the origin of the error


2. throw new Exception("錯誤訊息" + e.ToString());
preserve the information regarding the origin of the error while generating lengthy message


3. throw new Exception("錯誤訊息 + e.Message", e);
passing e as an inner exception helps preserve information while creating concise message regarding the error


4. throw e;
cannot preserve information

沒有留言: